Presentation Transcript
Slide 1:String
String:
A collection of characters
Character string:
character string are often used to built meaningful and readable program. The common operations performed on character string are
* Reading & writing string
* combined string together
* copying one string to another
* comparing string for equality
* extracting a portion of string
Slide 2:Declaring & Initializing string variables:
General form,
type string – name [size]
A string variable is any valid C variable name, and is always declared as an array.
Type specify the type of data.
Size determines the number of character in string name.
Eg: char name [20];
When a complier assigns a character string to a character array it automatically supplies the null character(0) at the end of string. Therefore the size should be equal to the maximum number of character in a string +1.
The character array may be initialized in two forms
String char name [9] = “new”;
Static char name [9] = {‘n’, ’e’, ’w’};
Slide 3:Reading string from terminal:
The input function scanf can be used with %s to read the string of characters.
char address [15];
scanf (“%s”, address);
The problem with scanf function is that it terminates its input in the first wide space it finds.
NEW YORK
Here the only string “new” will be read into the array address. Since the blank space after the word new will terminate the string.
In case of character array ‘&’ is not required before the variable name.
If we want to read the entire line “NEW YORK”, we have to use two character array of appropriate size.
scanf (“%s %s”, adr1,adr2);
It will assign “NEW” to adr1, and “YORK” to adr2
Slide 4:Simple Program:
#include
main()
{
char s1,s2,s3;
s1 = welcome;
s2 = to;
s3 = world;
printf (“%s %s %s %s”,s1,s2,s3,s4);
}
Slide 5:#include
main()
{
char s1 [10], s2 [10],s3 [10];
printf (“enter the text:”);
scanf (“%s”, s1);
scanf (“%s”, s2);
scanf (“%s”, s3);
printf (“s1=%s/n, s2= %s/n, s3= %s/n);
}
Slide 6:Reading a line of text:
It is not possible to use scanf function to read a line containing more than one word. This is because scanf terminates reading as soon as space is encountered in input.
To read a single character from the terminal using the function getchar(). We can use this function repeatedly to read single character from the input and place them into a single character array.
Thus a entire line of text can be read and stored in an array. The reading terminated when the new line character(\n) is entered, and a null character(\0) is then inserted at the end of the string
Slide 7:String Handling function:
C library supports a large number of string handling function that can be used to do string manipulations.
* strcat()
* strcmp()
* strcpy()
* strlen()
Slide 8:strcat():
The string cat function joins two strings together. It takes a following form.
General form,
strcat()
strcat(string1, string2);
String1 & string2 are character arrays string2 is appended to string1. It can be done by removing the null character at the end of string one end and placing string to form their.
Example:
s1 = A L L
s2 = W E L C O M E
Output: A L L W E L C O M E
Slide 9:strcmp():
The string compare function compares two string identified by their arguments, if there any difference between two character the statement should be “FALSE”, if the character is same the statement should be “TRUE”.
General form,
strcmp(string1,string2);
Example:
strcmp(“their”,”there”);
Slide 10:strlen():
This function is used to find the length of the string.
General form
n = strlen(string);
where n is an integer value which receives the value of the length of the string.
Example:
n = strlen(“hai”);