String functions in C Programming

The string is the collection of letters, alphabets, numbers special symbols, expressions, etc. enclosed inside double or single quotes. The string can be handled quite effectively in C programming. There are various string library functions defined under the header file string.h. To use the function we have to include a string header file at the preprocessor directive. Some of the common string functions areas:
 

 
String functions in C Programming
 

i. strlen();

This function gives the number of characters as an output, excluding the terminating null character. A string constant or array of characters can be passed as arguments for this function. Eg. if a given string is ‘Computer’ then it returns 8 since there are 8 characters in the string ‘Computer’.
 
Syntax:
 
strlen(string_constant or array_of_string);
 
 
    // program using built in function to find the length of string using C programming
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void main()
    {
        char str[40];
        int len;
        printf("Enter a string: ");
        scanf("%s", str);
        len = strlen(str);
        printf("The string %s has %d character n", str, len);
        getch();
    }
 

ii. strcat();

This function combines two strings together to form a single string. Two strings are passed as arguments into this function. Eg. if given strings are ‘computer’ and ‘science’ then it combines as ‘computerscience’.
 
Syntax:
 
strcat(destination_string, source_string);
 
 
    // program using built in function for concatenated string using C programming
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void main()
    {
        char str1[10], str2[10];
        printf("Enter the first string: ");
        scanf("%s", str1);
        printf("Enter the second string: ");
        scanf("%s", str2);
        strcat(str1, str2);
        printf("The concatenated string is :%s", str1);
        getch();
    }
 

iii. strcmp();

This function compares two strings such that if two strings are equal then this function returns 0 else if returns -1 or 1. If enter string is ‘COMPUTER’ and ‘computer’ it returns 0 because both strings are not exactly matched one is a capital letter and another is in a small letter.
 
Syntax:
 
strcmp(string1,string2);
 
 
// program using built in function for compare two strings string using C programming
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
    char str1[10], str2[10];
    int x;
    printf("Enter two strings :");
    scanf("%s%s", str1, str2);
    x = strcmp(str1, str2);
    if (x == 0)
        printf("string are equal");
    else if (x == -1)
        printf("String1 < strin2");
    else
        printf("string1 > string2");
    getch();
}
 

iv. strrev();

This function is used to reverse the given string i.e. convert the string in opposite form. Eg. if enter string is “computer” then it reverse as “retupmoc”.
 
Syntax:
 
strrev(String);
 
    // WAP to reverse given string using C programming language.
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    main()
    {
        char ch[50];
        printf("Enter a string to be reversedn");
        scanf("%s", &ch);
        strrev(ch);
        printf("The reversed string is: %s", ch);
        getch();
    }
 

v. strcpy();

This function is used to copy source_string to destination_string i.e. makes a duplicate copy of the string.
 
Syntax:
 
strcpy(destination_str,source_str);
 
    // program using built in function for copy one string to another using C programming
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void main()
    {
        char str1[10], str2[10];
        printf("Enter string :");
        scanf("%s", str2);
        strcpy(str1, str2);
        printf("The copied string is :%s", str1);
        getch();
    }
 

vi. strlwr();

This function is used to convert the uppercase character into lowercase. If any of the characters are already in lowercase then it skips that character. If enter string is “COMPUTER” then it returns as a computer.
 
Syntax:
 
strlwr(string);
 
    // program using built in function for converting uppercase into lowercase using C programming
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void main()
    {
        char str[30];
        printf("Enter uppercase string; ");
        scanf("%s", str);
        strlwr(str);
        printf("The lowercase string: n%s", str);
        getch();
    }
 

vii. strupr();

This function is used to convert the lowercase character into uppercase. If any of the characters is already in uppercase then it skips the character. If enter string is “computer” then it returns “COMPUTER”.
 
Syntax:
 
strupr(string);
 
    // program using built in function for converting lowercase into uppercase using C programming
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void main()
    {
        char str[30];
        printf("Enter lowercase string; ");
        scanf("%s", str);
        strupr(str);
        printf("The uppercase string: n%s", str);
        getch();
    }
 

Solved Program

 
    //WAP to check whether the given string is palindrome or not.
    #include <stdio.h>
    #include <string.h>
    int main()
    {   char a[10],b[10];
        printf("Enter string");
        scanf("%s",a);
        strcpy(b,a)
        strrev(a);
        if(strcmp(a,b)==0)
            {
                printf("%s is palindrome”,b);
            }
            else
            {
    printf("%s is not palindrome”,b);
            }
        return 0;
    }
 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *