Monday, 15 October 2012

String Handling Function in C

1) strlen() in C :

This function is used to determine the length of a given string.
 Syntax :
variable=strlen(“string”);
The string length will be returned to the variable.
Sample Program
str_len.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char str[50];
            int len;
            printf("Enter a string : ");
            scanf("%s",&str);

            len = strlen(str);
            printf("\n Length of the Given String is : %d",len);

            getch();
}
Output:
Enter a string : Hello
Length of the Given String is : 5
2) strcat() in C :
This function is used to join (concatenate) two strings.
Syntax :
strcat(string1,string2);
Sample program :
str_cat.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char str1[50], str2[50];
            printf("Enter first string : ");
            scanf("%s",&str1);
           
            printf("Enter second string : ");
            scanf("%s",&str2);
           
strcat(str1,str2);
printf("Concatinated String : %s",str1);

getch();
}
Output:
Enter first string : Good
Enter second string : Morning
Concatinated String : GoodMorning
Click Here to Download 

3) strcmp() in C :
This function is used to compare two strings. They are case sensitive.
Syntax :
strcmp(string1,string2);
If both the strings are equal return value will be 0, else non_zero value will be returned.
Sample program :
str_cmp.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char str1[50],str2[50];
            int n;

            printf("Enter string one : ");
            scanf("%s",&str1);
            printf("Enter string two : ");
            scanf("%s",&str2);

            n=strcmp(str1,str2);
            if(n==0)
            {
                        printf("Both the Strings are Equal");
            }
            else
            {
                        printf("Strings are not Equal");
            }
            getch();
}
Output :
Enter string one :Hello
Enter string two :Hello
Both the Strings are Equal
Click Here to Download 

4) strcpy() in C :
This function copies the string from one variable to another variable.
Syntax :
strcpy(source_variable, destination_variable);
Sample Program
str_cpy.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char str1[50], str2[50];
            printf("Enter a string : ");
            scanf("%s",&str1);
            strcpy(str2,str1);
            printf("Value of str1 : %s",str1);
            printf("\nValue of str2 : %s",str2);
            getch();
}
Output :
Enter a string : Good Morning
Value of str1 : Good Morning
Value of str2 : Good Morning
Click Here to Download 

5) strrev() in C :
This function is sued to reverse the characters in a given string.
Syntax :
strrev(string);
Sample program :
str_rev.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char str[50];
            printf("Enter a string : ");
            scanf("%s",&str);
            strrev(str);

            printf("Reverse String : %s",str);

getch();
}
Output:
Enter a string : hello
Reverse String : olleh
Click Here to Download 

6) strset() in C :
This function replaces all the characters of a string with a given symbol or character.
Syntax :
strset(string,symbol);
Sample program :
str_set.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char str1[30],str2;
            printf("Enter a string: ");
            gets(str1);

            printf("Enter a symbol to replace the string : ");
            scanf("%c",&str2);

            strset(str1,str2);

            printf("After strset : %s",str1);

            getch();
}
Output:
Enter a string: Good
Enter a symbol to replace the string : *
After strset : ****
Click Here to Download 

No comments:

Post a Comment