String
: A string is a sequence of characte that is treated as a single data item.->
Functions used in string:-
1. scanf() : It is a input function can be used with "%s" format specification to read in a string of character.
Example:
char address[10]
scanf("%s", address);
2. getchar(): Use of this function to read single character from the input and place them in to character array.
Example:
char ch;
ch = getchar();
'getchar' function has no parameters.
3. gets() : It is a library function available in the <stdio.h> header file2 is used to read character in to string from the keyboard.
This is a simple function wiyh one string parameter.
gets(str)
Example:
char line [80];
gets (line);
printf("%s", line);
4. printf() : Used this function with "%s" format to print string to the screen.
Example:
printf("%s", name);
5. putchar() : This function is ysed to write character to the screen. And can use this function repeatedly to output a string
of character stored in an array using a loop.
->Function putchar requires one parameter.
Example:
char name[6] = "PARIS";
for (i=0; i<5; i++)
putchar(name[i]);
6. puts() : This function is used for printing string values is to use the function 'puts' declared in the header file
<stdio.h>.
This is a one parameter function.
puts(str);
String-Handling Functions:
1. strcat() : This functon join two string together. It takes the following form:
strcat(string1 , string2);
2. strcmp() : The strcmp function compares two strings identified by the arguments and hae the value 0 if they
are equal.
strcmp(string1 , string2);
3. strcpy() : The strcpy function works almost like a string-assignment operator i.e is used copy second string in to
first string. It take forms :
strcpy(string1 , string2);
4. strlen() : The strlen function is used for count(count length of string) and returns the number of character in a
string. It takes form:
n = strlen(string);
where n is a integer variable , which recieve the value of the length of the string.
The counting ends at the first null chartacter.
Other String Function:
1. strncpy : This function copies only the left-most n characters of the surce string to the terget string variable.
This is the three-parameter function and is invoked as follows:
strncpy(s1, s2, 5);
2. strncmp : A variation of the function strcmp is the strncmp. This function has three-parameters as like:
strncmp(s1, s2, n);
this compares the left-most n character of s1 to s2 and returns.
(a) 0 if they are equal;
(b) negative number, if s1 substring is less then s2;
(c) positive number, otherwise.
3. strncat : This is another concatenation function tht takes three parameter as shown below:
strncat(s1, s2, n);
this call will concatenate the left-most n character of s2 to the end of s1.
4. strstr : It is a two parameter function that can be used to locate sub-string in a string. This takes the forms:
strstr (s1, s2);
strstr (s1, "ABC");
No comments:
Post a Comment