String manipulation function in C

String manipulation function in C

 String manipulation function in C

C programming contains library and built-in function for string manipulation. Sting functions define under the header file #include<string.h>

Some basic String functions are 

strlen()

strlen():- This function takes a string as a parameter and returns the integer value as the length of a string containing all the characters.

Syntax of strlen()

int variable=strlen(string);

Example of strlen()

Write a C program to find the length of string

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

                int l;

                char string[20];

                printf("\n Enter a string");

                scanf("%s",string);

                l=strlen(string);

                printf("\n Length of a string=%d", l);

                getch();

}

Output


strcpy()

strcpy():- The function strcpy() is use to copy one string to another string. It takes two strings as a parameter and copy the characters of the source string to the destination string.

Syntax of strcpy()

strcpy(destination string, source string);

Example of strcpy()

Write a C program to copy one string to another string

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

                char string1[20], string2[20];

                printf("\n Enter a string");

                scanf("%s",string1);

                strcpy(string2,string1);

                printf("\n Copy string=%s", string2);

                getch();

}

Output


strcat()

strcat():- This function is used to concatenate i.e. it appends a string to the end of next string. This function takes two string as a parameter and copy character by character of second string to the end of first string.

syntax of strcat()

strcat(string1,string2)

Example of strcat()

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

                char string1[20], string2[20];

                printf("\n Enter two string");

                scanf("%s%s",string1, string2);

                strcat(string1,string2);

                printf("\n Concatenate string=%s", string1);

                getch();

}

Output

strrev()

strrev():- strrev() function is use to reverse all the character. It takes one string as a parameter and reverses all the characters of a string except the null character.

Syntax of strrev()

strrev(string);

Example of strrev()

Write a c program to read a string and print reverse

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

                char string1[20];

                printf("\n Enter a string");

                scanf("%s",string1);

                strrev(string1);

                printf("\n Reverse string=%s", string1);

                getch();

}

Output



strupr()

strupr():- This function strupr() takes a string as a parameter and converts all alphabets of a string into capital letters.

Syntax of strupr()

strupr(string);

Example of strupr()

Write a C program to take a string and convert all alphabet into capital letters

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

                char string[20];

                printf("\n Enter a string");

                scanf("%s", string);

                strupr(string);

                printf("\n Capital string=%s", string);

}

Output


strlwr()

strlwr():- This function strlwr() takes a string as a parameter and convert all alphabets of a string into small letters.

Syntax of strlwr()

strlwr (string);

Example of strlwr()

Write a C program to read a string and convert all alphabets into small letters.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

                char string[20];

                printf("\n Enter a string");

                scanf("%s", string);

                strlwr(string);

                printf("\n Small letters=%s", string);

}

Output

strcmp()

strcmp():-
This function takes two string as a parameter and compares to each other. It returns an integer where the value is

(i)                  >0 -------- if first string is greater than second string

(ii)                <0---------If first sting is less than second string

(iii)               =0---------if both string are equal

Syntax of strcmp()

int variable = strcmp(string1, string2);

Example of strcmp()

Write a C program to, find greater string, and show if they are equal

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

                char string1[20], string2[20];

                printf("\n Enter two string");

                scanf("%s%s", string1,string2);

                if(strcmp(string1,string2)>0)

                {

                                printf("%s is greater than %s",string1, string2);

                }

                else if(strcmp(string1,string2)<0)

                {

                                printf("%s is greater than %s",string2,string1);

                }

                else

                {

                                printf("%s is equal than %s", string1,string2);

                }

                getch();

}

Output


Post a Comment

0 Comments