Built in String Functions and applications

The header file string.h (<string.h>) contains some string manipulation functions.


Strlen Function

The Strlen function returns the length of a string.It takes the string name as argument.

n=strlen(str);

where str is name of the string and n is the length of the string ,return by strlen function.

 Example
Write a program to read a string from the keyboard and display the length of the string on to the monitor by using strlen() function.

#include<stdio.h>
#include<string.h>

main()
{
char name[80];
int length;
printf("Enter your name:");
gets(name);
length=strlen(name);
printf("Your name has %d characters \n,"length);
}

OUTPUT
Enter your name: TYRAN
your name has 5 characters

Strcpy Function

In C,you cannot simplify assign one character array to another .You have to copy element by element.The string library <string.h> contains a function called strcpy for this purpose.The strcpy function is used to copy one string to another .The syntax is as follows:

strcpy(str1,str2);

where str1,str2 are two strings.the content of string str2 is copied on to string str1.

EXAMPLE

Write a program to read a string from the keyboard and  copy 5the string onto the second string and display the strings on to the  monitor by using strcpy() function.

#include<stdio.h>
#include<string.h>
main()
{
char first[80],second[80];
printf("Enter a string :");
 gets(first);
strcpy(second,first);
printf("\n First string is : %s,and second string is : %s\n",first,second);
}

OUTPUT
Enter a string : HONEY
First string is : HONEY, and second string is : HONEY

Strcmp Function

The strcmp function in the string library function which compares two strings, character by character and stops comparison when there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the characters that is integer.If the return value zero means the two strings are equal ,a negative value means the first string is less than second,and a positive value means first greater than second.

n=strcmp(str1,str2);

where str1 and str2 are two strings to be compared and n is the returned value of differed characters.

EXAMPLE

Write a program to compare two strings.

#include<stdio.h>
#include<string.h>
main()
{
 char first[80],char second[80];
int value;
printf("Enter a string:");
gets(first);
printf("Enter another string:");
gets(second);
value=strcmp(fisrt,second);
if(value==0)
puts("The two strings are equal");
else if (value<0)
puts("The first  string is smaller");
else if (value>0)
puts("the first string is bigger");
}

OUTPUT
Enter a string :DOG
Enter another string :DOGGY
The first string is smaller


Strrev Function 

The strrev function reverse the given string.

strrev(str);

where string str will be reversed.

EXAMPLE

Write a program to reverse  a given string.

#include<stdio.h>
#include<string.h>

main()
{
char first[80];
printf("Enter a string:");
gets(first);
printf("\n Reverse if the given string is : %s",strrev(first));
 } 
 
 OUTPUT
Enter a string:PANKAJ
Reverse of the given string is :JAKNAP

Strspn Function

The  strspn function returns the position of the string,where first string mismatches with second string.

n=strspn(first,second);

where  first and second are two strings to be compared ,n is the number of character from which first string does not match with second string.

#include<stdio.h>
#include<string.h>

main()
{
char first[80],second[80];
printf("Enter first string:");
gets(first);
printf("\n Enter second string:");
gets(second);
printf("\n After %d characters there is no match ",strspn(first,second));
}

OUTPUT
Enter first string:ALEXANDER
Enter second string:ALEXSMITH
After 4 characters there is no match.
 

0 comments:

Post a Comment