String Functions in C/C++

String Functions in C/C++

String functions in C/C++ are used to perform various tasks. Normally when we have to concatenate, compare, copy, etc two strings we have to use for loop. And due to this the length of program increases. String functions not only reduces size of program but also simplifies the logic of the program. Most common string functions are strlen(), strcpy(), strstr(), strcat(), strcmp(), strrev(), etc.

In order to use string function we have add string.h header file. Without this file you will get an error. Let’s see all the string functions.

strlen() : This function calculates size of the string.
strcmp() : This function compares two strings.
strcpy() : It copies one string to another.
strcat() : It concatinates (joins) two strings.
strrev() : It reverses the string.
strlwr() : It converts the string to into lowercase.
strupr() : It converts the string to uppercase.
strncmp() : It compares first n characters of two strings.
strcmpi() : It compares two string without considering case.
stricmp() : It compares two string without considering case. (Identical to strcmpi)

strnicmp() : It compares first n characters of two strings. It is not case sensitive.
strncpy() : It copies first n characters of one string into another.
strncat() : It appends first n characters of a string to the end of other string.
strdup() : It duplicates a string.
strchr() : It finds out first occurence of a given character in a string.
strrchr() : It finds out last occurence of a given character in a string.
strstr() : It finds out first occurence of a given string in another string.
strset() : It sets all characters of a string to a given character.
strnset() : It sets first n characters of a string to a given character.

Each functions takes atleast one argument.

Q. Write C/C++ programs to demonstrate string functions.

strlen() & strrev() functions-

C Code :-

//strlen and strrev functions

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

int main()
{
    int len;
    char str1[50];

    printf("\n Enter a string : ");
    gets(str1);

    printf("\n Entered string is : %s", str1);

    len = strlen(str1);    //Calculating length of entered string.
    printf("\n Length of entered string is : %d", len);

    strrev(str1);   //Reversing the string
    printf("\n Reverse of enetered string is : %s",str1);

    return 0;
}

C++ Code :-

//strlen and strrev functions

#include<iostream>
#include<string.h>

using namespace std;

int main()
{
    int len;
    char str1[50];

    cout<<"\n Enter a string : ";
    gets(str1);

    cout<<"\n Entered string is : "<<str1;

    len = strlen(str1);    //Calculating length of entered string.
    cout<<"\n Length of entered string is : "<<len;

    strrev(str1);   //Reversing the string
    cout<<"\n Reverse of enetered string is : "<<str1;

    return 0;
}

Output :-

strcpy strstr strcat strcmp strrev

strcat() function-

C Code :-

// TO concatenate two strings i.e. join two strings

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

int main()
{
    char str1[50], str2[50];

    printf("\n Enter first string : ");
    gets(str1);

    printf("\n Enter second string : ");
    gets(str2);

    printf("\n Entered strings are : \n 1)%s \n 2)%s", str1, str2);

    strcat(str1, str2);   //Appending string stored in str2 to string stored in str1 

    printf("\n\n Final string is : %s", str1);

    return 0;
}

C++ Code :-

// TO concatenate two strings i.e. join two strings

#include<iostream>
#include<string.h>

using namespace std;

int main()
{
    char str1[50], str2[50];

    cout<<"\n Enter first string : ";
    gets(str1);

    cout<<"\n Enter second string : ";
    gets(str2);

    cout<<"\n Entered strings are :\n 1)"<< str1 <<"\n 2)" << str2;

    strcat(str1, str2);   //Appending string stored in str2 to string stored in str1 

    cout<<"\n\n Final string is : "<< str1;
   
    return 0;
}

Output :-

strcpy strstr strcat strcmp strrev

strcpy() function-

C Code :-

//To copy one string into another using string function

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

int main()
{
    char str1[50], str2[50];

    printf("\n Enter a string : ");
    gets(str1);

    strcpy(str2, str1);  //Copying string stored in str1 to str2

    printf("\n String entered by user is : %s", str1);
    printf("\n Copied string is : %s", str2);

    printf("\n\n");
    return 0;
}

C++ Code :-

//To copy one string into another using string function

#include<iostream>
#include<string.h>

using namespace std;

int main()
{
    char str1[50], str2[50];

    cout<<"\n Enter a string : ";
    gets(str1);

    strcpy(str2, str1);  //Copying string stored in str1 to str2

    cout<<"\n String entered by user is : "<< str1;
    cout<<"\n Copied string is : "<< str2;

    return 0;
}

Output :-

strcpy strstr strcat strcmp strrev

strcmp() function-

C Code :-

//Comparing two strings using strcmp function

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

int main()
{
    int ans;
    char str1[50], str2[50];

    printf("\n Enter a string : ");
    gets(str1);

    printf("\n Enter another string : ");
    gets(str2);

    printf("\n Entered strings are : \n 1)%s \n 2)%s", str1, str2);

    ans = strcmp(str1, str2);      //Comparing two strings

    if(ans == -1)     //i.e. if str1 < str2
    {
        printf("\n\n First string is smaller than second string");
    }
    else if(ans == 0)    //i.e. if str1 == str2
    {
        printf("\n\n Both strings are equal");
    }
    else      //i.e. if ans == 1  and str1 > str2
    {
        printf("\n\n First string is greater than second string");
    }

    return 0;
}

C++ Code :-

//Comparing two strings using strcmp function

#include<iostream>
#include<string.h>

using namespace std;

int main()
{
    int ans;
    char str1[50], str2[50];

    cout<<"\n Enter a string : ";
    gets(str1);

    cout<<"\n Enter another string : ";
    gets(str2);

    cout<<"\n Entered strings are :\n 1)"<< str1 <<"\n 2)" << str2;

    ans = strcmp(str1, str2);      //Comparing two strings

    if(ans == -1)   //i.e. if str1 < str2
    {
        cout<<"\n\n First string is smaller than second string";
    }
    else if(ans == 0)    //i.e. if str1 == str2
    {
        cout<<"\n\n Both strings are equal";
    }
    else    //i.e. if ans == 1  and str1 > str2
    {
        cout<<"\n\n First string is greater than second string";
    }

    return 0;
}

Output :-

strcpy strstr strcat strcmp strrev

Program Parts –

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. string is header file to execute string related functions. Without this file strcmp, strcpy, etc functions will not execute.
4. .h is an extension for header file.
5. int is data type. It indicates that the program returns a value.
6. main() is main function that indicates the compiler that the user written programs starts from here.
7. print/cout is the keyword use to print a message.
8. scanf/cin is the keyword use to store values in variables.
9. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion.

So strcpy, strstr, strcat, strcmp, strrev are some most common string functions in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

1 COMMENT

Leave a Reply

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