String handling functions


String handling functions

            These are the functions supported by <string.h> library function and used for string manipulations.


String Handling Function
Meaning
strlen
Length of the string
strrev
Reverse of the string
strcat
Concatenates two strings
strcmp
Compares two strings
strcpy
Copies one string over another
strlwr
Converts string into lowercase letters
strupr
Converts string into uppercase letters
strchr
Searches for single character
strstr
Searches for sub string



strlen( )  This function is used to determine the length of a string.

Syntax:
            strlen(string);

Here the string can be either string variable or string constant. The length does not include null character.

Sample program to determine the length of the string


#include<string.h>
void main()
{
 char a[10]="krishna";
 int n;
 n=strlen(a);
 printf("\n Length of a= %d",n);
 printf("\n Length= %d",strlen("abcd"));
}
Output
 Length of a= 7
 Length= 4

strrev( )  This function is used to reverse a string.
Syntax:
            strrev(string);

Here the string will be reversed and stored in same string and string can be either string variable or string constant.

Sample program to reverse the string
#include<string.h>
void main()
{
 char a[10]="krishna";
 strrev(a);
 printf("\n reversed string= %s",a);
 printf("\n Reversed string= %s",strrev("abcd"));
}
Output
 Reversed string= anhsirk
 Reversed string= dcba


strcat( )  This function is used to concatenate two strings.

Syntax:
            strcat(string1,string2);

Here string2 will be appended at the end of string1 after removing the null character of string1 and the result will be stored in string1.Here string1 must be the string variable and string2 can be either string constant or string variable.

Sample program to concatenate two strings
#include<string.h>
void main()
{
 char a[20]="naga",b[10]="raju";
 strcat(a,"->");
 strcat(a,b);
 printf("\n New string= %s",a);
 }
Output
 New string= naga->raju
strncat()       

            This is another concatenation function where only leftmost n characters of string2 will be appended to string1 after removing the null character of string1 and result will be placed in string1.

Syntax:
            strncat(string1,string2,n);

Program
#include<string.h>
void main()
{
 char a[10]="sri",b[15]="harinivas";
 strncat(a,b,4);
 printf("\n New string= %s",a);
}

Ouput
 New string= srihari

strcmp( )

This function compares two strings with case sensitivity and returns 0 if the strings are same, other wise it returns the ASCII value difference of the unmatched character.

syntax:            strcmp(string1,string2);
Here both string1 and string2 can be either string variable or string constant.
Program
#include<string.h>
void main()
{
 char a[10]="abcd",b[10]="ABcd";
 int n;
 n=strcmp(a,b);
 if(n==0)
  printf("\n strings are equal");
 else
  printf("\n Strings are not equal and return value: %d",n);
}
Output 
 Strings are not equal and return value: 32

strncmp( )
This function compares first n characters in two strings with case sensitivity and returns 0 if the strings up to n characters are same, other wise it returns the ASCII value difference of the unmatched character.
Syntax:           strncmp(string1,string2,n);     

Program
#include<string.h>
void main()
{
 char a[10]="abcd",b[10]="abCDefg";
 int n;
 n=strncmp(a,b,2);
 if(n==0)
  printf("\n strings are equal");
 else
  printf("\n Strings are not equal and return value: %d",n);
}
Output
 Strings are equal
stricmp( )
This function compares two strings with out case sensitivity and returns 0 if the strings up to n characters are same, other wise it returns the ASCII value difference of the unmatched character.
Syntax:
            stricmp(string1,string2);         

Program
#include<string.h>
void main()
{
 char a[10]="abcd",b[10]="ABCD";
 int n;
 n=stricmp(a,b);
 if(n==0)
  printf("\n strings are equal");
 else
  printf("\n Strings are not equal and return value: %d",n);
}
Output
 Strings are equal
strnicmp( )

This function compares first n characters in two strings with out case sensitivity and returns 0 if the strings up to n characters are same, other wise it returns the ASCII value difference of the unmatched character.
Syntax:                       strnicmp(string1,string2,n);    
Program
#include<string.h>
void main()
{
 char a[10]="abcd",b[10]="ABcdefg";
 int n=0;
 n=strnicmp(a,b,2);
 if(n==0)
  printf("\n strings are equal");
 else
  printf("\n Strings are not equal and return value: %d",n);
}
Output
 Strings are equal

strcpy( )
This function works like string assignment operator. This function copies string from source to destination.
Syntax:
            strcpy(string1,string2);

Here string1 will be known as destination string and string2 will be known as source string. Here string2 will be copied into string1 and string1 must be string variable and string2 can be either string variable or string constant.
Program
#include<string.h>
void main()
{
 char a[10]="Krishna",b[10],c[10];
 strcpy(b,a);
 puts(b);
 strcpy(c,"Rama");
 puts(c);
}
Output  Krishna
 Rama

strncpy( )
This function copies the specified number of characters from the source string to destination string.
syntax:
            strncpy(string1,string2,n);
Here n characters from string2 will be copied into string1.

Program
#include<string.h>
void main()
{
 char a[10]="Krishna",b[10];
 strncpy(b,a);
 puts(b);
}

Output
 Krish

strlwr( )

This function is used to convert the upper case alphabets to its equivalent lower case.
Syntax:
            strlwr(string1);
 Here string1 can be either string constant or string variable.

Program
#include<string.h>
void main()
{
 char a[10]="RAMA";
 strlwr(a);
 puts(a);
 printf("%s",strlwr("KRISHNA"));
}
Output
rama
krishna

strupr( )

This function is used to convert the lower case alphabet to its equivalent upper case.
Syntax:
            strupr(string1);
 Here string1 can be either string constant or string variable.

Program
#include<string.h>
void main()
{
 char a[10]="rama";
 strupr(a);
 puts(a);
 printf("%s",strupr("krishna"));
}

Output
RAMA
KRISHNA

strchr()

This function scans the given string for the first occurrence of given character. If the character was found, this function returns a pointer to the first occurrence of the character in the given string otherwise returns null.
Syntax:
            strchr(string, character);

Here this function searches for the first occurrence of the character in given string.

Program
void main()
{
 char a[10],ch,*p;
 printf(" Enter string: ");
 gets(a);
 printf(" Enter character to search: ");
 ch=getche();
 p=strchr(a,ch);
 if(p)
  printf("\n %c found at %d location",ch,p-a+1);
 else
  printf("\n %c not found",ch);
}

Output
 Enter string: srinivas
 Enter character to search: n
 n found at 4 location

strstr()

This function scans the given string for the first occurrence of given substring. If the substring was found, this function returns a pointer to the element in string where substring begins otherwise returns null.
Syntax:
            strstr(string, substring);
Here this function searches for the first occurrence of the given substring in given string.


Program
#include<string.h>
void main()
{
 char a[10],b[6],*p;
 printf(" Enter string: ");
 gets(a);
 printf(" Enter substring to search: ");
 gets(b);
 p=strstr(a,b);
 if(p)
  printf(" %s found at %d location",b,p-a+1);
 else
  printf(" %s not found",b);
}

Output
 Enter string: srinivas
 Enter substring to search: iva
 iva found at 5 location

No comments:

Post a Comment