STANDARD LIBRARY A STRING FUNCTION
With every c compiler, a large set of useful string handling library function are provided.
In c we have few function that we can use without creating them.
Function |
Use |
Strien |
Find length of a string |
Striwr |
Converts a string to lower case |
Strupr |
Converts a string to upper case |
Strcat |
Append one string at the end of another |
Strncat |
Append first character of string at the end of another |
Strcpy |
Copies a string into another |
Strncpy |
Copies first character of one string into another |
Strcmp |
Compare two string |
Strncmp |
Compare first character of two string |
Strcmpi |
Compare two string without regard to case(“I denotes that this function ignores case”) |
Stricmp |
Compose two string without regard to case |
Strnicmp |
Compose first character of two string without regard to case duplicates a string |
Strdup |
Duplicates a string |
Strctr |
Finds first accurence of a given character in a string. |
Strrctr |
Finds last accurence of a given character in a string. |
Strstr |
Finds first accurence of a given string in another string. |
Strset |
Sets all character of a string to a given character. |
Strnset |
Sets first n characters of a string to a given character. |
Strrev |
Reserve string. |
Strlen-> this function count the number of character present in the string
Ex
#include<stdio.h>
#include<string.h>
int main()
{
char array[]={“geeks with geeks”};
int len1, len2;
len1=strlen(array);
len2=strlen(GWG);
printf(“string %s length = %d/n”, array, len1);
printf(“string %s length = %d/n”, GWG, len2);
return 0;
}
Strcpy-> this function copies the content of one string in to another
#include<string.h>
int main()
{
char source[]={“geeks with geeks”};
char target[20];
strcpy(target, source);
printf(“source string= %s /n”, source);
printf(“target string= %s /n”, target);
return 0;
}
Output:
source string = geeks with geeks
target string = geeks with geeks
strcat-> this function add the source string in to end of target string
#include<string.h>
int main()
{
char source[]={“vikas”};
char target[]=”hello”;
strcat(target, source);
printf(“source string= %s /n”, source);
printf(“target string= %s /n”, target);
return 0;
}
Outout:
source string=vikas
target string = hello vikas
strcmp-> this function compare two string to find out where they are same or different
#include<string.h>
int main()
{
char source[]={“vikas”};
char target[]=”hello”;
int I, j, k;
i=Strcmp( source, “vikas”);
j=strcmp(source, target);
k=strcmp(source, “vikas boy”);
printf(“%d %d %d”, I, j ,k);
return 0;
}
Output: 04 - 32
summary
1.a string is nothing but array of character terminated by ‘\o’.
2.being an array all the characters of a string are stored in memory location.
3.thugh scan f is used to receive multi word string.
4.both print f and puts can handle multi word string.