STRING MANIPULATION FUNCTIONS FROM THE STANDARD LIBRARY

Strings are often needed to be manipulated by programmer according to the need of a problem. All string manipulation can be done manually by the programmer but, this makes programming complex and large. To solve this, the C supports a large number of string handling functions. There are numerous functions available in <string.h> header file. It includes functions to perform useful tests and manipulations on Strings. Each of these functions receives a pointer to character as an argument. Commonly used String handling functions are:

1. unsigned int strlen(const char *s);
2. char * strcpy(char *dest, const char *src);
3. char * strcat(char *dest, const char *src);
4.char * strcmp(const char *dest, const char *src);
5.0char * strchr(const char *s, int c);
6. char * strstr(const char *m_str, const char *s_str);
7. char * strrev(char *str);
strlen()

This function calculates the length of string. It takes only one argument, i.e., string-name.
The syntax is : temp_variable = strlen(string_name);

 Program to illustrate the use of strlen().
#include<string.h>
#include<stdio.h>
void main()
{
char c[20]; int len;
printf("Enter string whose length is to be found:");
gets(c);
len=strlen(c);
printf("\n Length of the string %s is %d ",c, len);
}
Output: Enter string whose length is to be found: program
Length of the string program is 7
strcpy()
This function copies the content of one string to the content of another string. It takes 2 arguments. The syntax is : strcpy(destination,source);
where source and destination are both the name of the string.
Example: Program to illustrate the use of strcpy().
#include<string.h>
#include<stdio.h>
void main()
{ char src[20],dest[20];
printf("Enter string: ");
gets(src);
strcpy(dest, src); //Content of string src is copied to string dest
printf("Copied string: "); puts(dest);
}
STRING MANIPULATION FUNCTIONS FROM THE STANDARD LIBRARY STRING MANIPULATION FUNCTIONS FROM THE STANDARD LIBRARY Reviewed by Unknown on 04:49 Rating: 5

No comments:

Powered by Blogger.