Sie sind auf Seite 1von 2

List of (most frequently used) string library functions in #include <string.

h>
c.
void main(void)
strlen strcpy strcat strrev strcmp strcmpi strupr strlwr
{
strncmpi strncat strncmp strncmpi strncmpi.
char src[]="spark",dest[]="programming";
String functions examples
strcat(dest,src);
1) int strlen(char array):This function accepts string as
parameter and return integer i.e printf("concatenated string is %s",dest);
the length of String passed to it. }
Example Output: concatenated string is programmingspark
#include <stdio.h> 4) strrev (string):This function accepts single string as
parameter and reverse that string.
#include <string.h>
Example
void main(void)
#include <stdio.h>
{
#include <string.h>
char string[]="spark";
void main(void)
int len;
{
len=strlen(string);
char string[]="spark";
printf("length of %s is %d\t", string, len);
strrev(string);
}
printf("reverse string is %s",string);
Output::length of spark is 5.
}
Did you notice that strlen() does not include '\n' in
string length or else length would be 6. Output: reverse string is kraps.
2) strcpy (Destination string,source string):This function 5)int strcmp (string 1, string2):This function compares
accepts 2 strings as two strings passed as
parameter,1st one is destination string and 2nd is parameters and returns either +ve number,0,-ve
source string. This function copies source number.
string to destination string. +ve value indicates string1 > string2.
Example 0 indicates string1 and string2 are equal
#include <stdio.h> -ve value indicates string1 < string2.
#include <string.h> Example
void main(void) #include <stdio.h>
{ #include <string.h>
char src[]="spark",dest[15]; void main(void)
strcpy(dest,src); {
printf("%s is copied to dest string\t",dest); char string1[]="spark",string2[]="programming";
} int cmp;
Output: spark is copied to dest string. cmp=strcmp(string1,string2);
3) strcat (Destination string,source string): This function if(cmp>0)
accepts two strings source
printf("%s > %s",string1,string2);
string is appended to the destination string.
else
Example
{
#include <stdio.h>
if(cmp<0) char string1[]="SPArk";

printf("%s < %s",string1,string2); strlwr(string1);

else printf("%s is in lower case",string1);

printf("%s = %s",string1,string2); }

} Output: spark is in lower case.

} 8) strupr (string)::This function accepts single string that


can be in any case(lower or
Output: spark > programming.
upper).Itconverts the string in upper case.
.this is because alphabetically p comes first then s.so
the string compare function returns Example

difference between ascii of s and p which would be +ve. #include <stdio.h>

6) strcmpi (string 1, string2): This function is similar to #include <string.h>


strcmp().The onlyy difference is that it ignores the
void main(void)
case.example SparK and spark both are same.
{
Example
char string1[]="SPArk";
#include <stdio.h>
strupr(string1);
#include <string.h>
printf("%s is in upper case",string1);
void main(void)
}
{
Output: SPARK is in upper case.
char string1[]="spark",string2[]="SPArk";

int cmp;

cmp=strcmpi(string1,string2);

if(cmp>0)

printf("%s > %s",string1,string2);

else

if(cmp<0)

printf("%s < %s",string1,string2);

else

printf("%s = %s",string1,string2);

Output: spark = SPArk.

7) strlwr (string)::This function accepts single string that


can be in any case(lower or

upper).Itconverts the string in lower case.

Example

#include <stdio.h>

#include <string.h>

void main(void)

Das könnte Ihnen auch gefallen