0% found this document useful (0 votes)
9 views27 pages

L11 Strings

This document is a lecture module on strings in C programming, explaining that strings are treated as character arrays ending with a null character. It covers how to declare and initialize strings, read strings from users, and various string functions such as puts(), gets(), strlen(), strcpy(), strcmp(), sprintf(), and sscanf(). Additionally, it emphasizes the importance of using reference books for detailed understanding of the concepts discussed.

Uploaded by

Ishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views27 pages

L11 Strings

This document is a lecture module on strings in C programming, explaining that strings are treated as character arrays ending with a null character. It covers how to declare and initialize strings, read strings from users, and various string functions such as puts(), gets(), strlen(), strcpy(), strcmp(), sprintf(), and sscanf(). Additionally, it emphasizes the importance of using reference books for detailed understanding of the concepts discussed.

Uploaded by

Ishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

C Programming

Module-11: String in C

IMT and IMG 1st Year

Instructor : Dr. Santosh Singh Rathore


ABV-IIITM Gwalior
Disclaimer

This is NOT A COPYRIGHT MATERIAL

Content for the slides have been taken from the various sources and
from the reference books.

Students need to follow reference books to get the thorough details


of the concepts discussed in slides
String
• There is no separate data type for strings in C.
• They are treated as arrays of type char.
• A character array is a string if it ends with a null character (\0).
• This null character is an escape sequence with ASCII value 0.
• For ex.:
How to Declare and Initialize String
Read the String from User
Passing String to Function
Strings and Pointers
Strings and Pointers
Strings and Pointers
Puts() and gets() functions
• C standard library provides the gets function.
• It allows us to read a line of characters (including spaces and tabs)
until the newline character is entered, i. e., the Enter key is pressed.
• A call to this function takes the following form: gets(s);
• where s is an array of char, i. e., a character string.
• The function reads characters entered from the keyboard until
newline is entered and stores them in the argument string s,
• The newline character is read and converted to a null character (\O)
before it is stored in s.
Puts() and gets() functions
• The C standard library provides another function named puts to
print a string on the display.
• A typical call to this function takes the following form: puts(s);
• where s is an array of char, i. e., a character string.
• This string is printed on the display followed by a newline
character.
Puts() and gets() functions
Srting.h header file and functions
Srting.h header file and functions
• The strlen() function takes a string as an argument and returns its length.
• The returned value is of type size_t (the unsigned integer type).
String.h header file and functions
• The function prototype of strcpy() is:
• char* strcpy(char* destination, const char* source);

When you use


strcpy(), the size
of the
destination string
should be large
enough to store
the copied string.
Otherwise, it
may result in
undefined
behavior.
String.h header file and functions
• The function prototype of strcmp() is:
• int strcmp (const char* str1, const char* str2);

In the program,

strings str1 and str2 are


not equal. Hence, the
result is a non-zero
integer.

strings str1 and str3 are


equal. Hence, the result
is 0.
Some Examples
Some Examples
• Program to count vowels, consonants, etc.
Some Examples
• Program to count vowels, consonants, etc.

Output
Enter a line of string:
C++ 20 is the latest
version of C++ yet.

Vowels: 9

Consonants: 16

Digits: 2 White

spaces: 8
Some Examples
• Creation of srtlen() Function
Some Examples
Creation of
srtcmp()
Function
sprintf() function
int sprintf (char *str, const char *controlstring [, argument!, argument2, ,.........] );
•This function is same as printf ( ) function except that instead of sending the
formatted output to screen, it stores the formatted output to the string.
•With the help of this function, we can convert variables of any data type to string.
•A null character is appended at the end automatically.
•This function returns the number of characters output to the string excluding the
null character.
•It is the responsibility of the programmer to take the size of string large enough to
store all the arguments, so that no overflow occurs.
sprintf() function
sprintf() function
sscanf() function
int sscanf (const char *str, const char *controlstring [, address 1, address2, …];
•This function is same as the scanf () function except that data is read from a string
rather than the standard input.
•We can read the formatted text from a string and convert it into variables of
different data types.
sscanf() function
Thank you for your attention!

< Questions?

You might also like