PreDefined String Functions in C

PreDefined String Functions in C Language with Examples

In this article, I am going to discuss the PreDefined String Functions in C Language with Examples. Please read our previous articles, where we discussed Strings in C Language with Examples. 

Strings related pre-defined functions are declared in string.h:
  1. strcpy()
  2. strlen()
  3. strrev()
  4. strcat()
  5. strupr()
  6. strlwr()
  7. strcmp()
  8. stricmp()
  9. strstr()
strcpy():

By using this predefined function, we can copy a string to another string. It requires 2 arguments of type(char*) & return(char*) only. When we are working with strcpy() from a given source address upto \0 entire content will be copied to the destination string.

Syntax: char*strcpy(char*dest, const char*src);

Program: To understand strcpy() function in C
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main() 
{    
    char s1[10]="hello"; 
    char s2[10]="welcome";
    puts(s1);
    puts(s2);
    strcpy(s2,s1);
    puts(s1);
    puts(s2);
    return 0; 
}
Output:

PreDefined String Functions in C Language with Examples

strlen():

By using this predefined function, we can find the length of the string. strlen() function requires 1 arguments of type (const char*) and returns an int type. When we are working with strlen() from the given address up to \0, the entire character count value will return.

Note: The length of the string means the total no. of characters excluding \0 characters. The size of the string means the total no. of characters including \0 characters.

Program: To understand strlen() function in C
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main() 
{    
    char str[]="welcome";
    int s,l;
    s=sizeof(str);
    l=strlen(str);
    printf("\nsize: %d",s);
    printf("\nlength: %d",l);
    getch();
    return 0; 
}
Output:

strlen() function in C

strrev():

By using this predefined function, we can reverse the string. strrev() requires 1 argument of type (char*) and returns (char*). When we are working with sterrev() from given address up to null entire string data will make reverse except for the null character.

Syntax: char*strrev(char*str);

Program: To understand strrev() function in C
#include <stdio.h>
#include<conio.h> 
#include<string.h>
int main()
{
    char str[30]=”Welcome”;
    puts(str);
    strrev(str);	
    printf("\n Reverse String is : %s", str);
    getch();
    return 0;	
}
Output:

strrev() function in C

strcat():

By using this predefined function, we can concatenate a string to another string. Concatenation means copying data from the end of the string i.e. appending process. strcat() requires 2 arguments of type (char*) and returns (char*) only.

Syntax: char*strcat (char*dest, const char*src);

When we are working with strcat() function, always appending will take place at the end of the destination only.

Program: To understand strcat() function in C
#include <stdio.h>
#include<conio.h> 
#include<string.h>
int main()
{
    char s1[15]="hello";
    char s2[30]="HI";
    puts(s1);
    puts(s2);
    strcat(s2,s1);	
    puts(s2);
    getch();
    return 0;	
}
Output:

strcat() function in C

strupr():

By using this predefined function, we can convert a string into an upper case. strupr() function requires 1 argument of type (char*) and returns (char*). When we are working with strupr() function, from given address up to null all lower case characters are converted into uppercase.

Syntax: char*strupr(char*str);

Program: To understand strupr() function in C
int main() 
{ 
    char str[ ] = "Welcome to C programming language"; 
    //converting the given string into uppercase. 
    printf("%s\n", strupr (str)); 
    return  0; 
}

Output: WELCOME TO C PROGRAMMING LANGUAGE

strlwr():

By using this predefined function, we can convert a string into a lower case. strlwr() function requires 1 argument of type (char*) and returns (char*). When we are working with strlwr() function, from given address up to null all upper case characters are converted into lowercase.

Syntax: char*strlwr(char*str);

Program: To understand strlwr() function in C
#include<stdio.h> 
#include<string.h> 
int main() 
{ 
    char str[ ] = "GOOD MORNING"; 
    //converting the given string into uppercase. 
    printf("%s\n", strlwr (str)); 
    return  0; 
}

Output: good morning

strcmp():

By using this predefined function, we can compare strings. strcmp() requires 2 arguments of type (const char*) & returns an integer value. When we are working with strcmp(), then character by character comparison takes place until the first unpaired character set has occurred. When the first unpaired character set has occurred then it returns an ASCII value difference. At the time of comparison if there is no difference, then it returns 0.

Program: To understand strcmp() function in C
#include <stdio.h>
#include<conio.h> 
#include<string.h>
int main()
{
    char s1[30]="hello";
    char s2[30]="hello";
    int d;
    puts(s1);
    puts(s2);
    d=strcmp(s1,s2);	
    printf("ASCII Value difference: %d",d);
    getch();
    return 0;	
}
Output:

strcmp() function in C

stricmp()

By using this predefined function, we can compare the strings without any case i.e. uppercase and lowercase contents both are treated the same. When we are working with strcmp() function it works with the help of case i.e. uppercase and lowercase content both are different. stricmp() required 2 arguments of type (const char*) & returns an int value.

Syntax: int stricmp (const char*s1, const char*s2);

Program: To understand stricmp() function in C
#include <stdio.h>
#include<conio.h> 
#include<string.h>
int main()
{
    char s1[30]="hello";
    char s2[30]="HELLO";
    int d;
    puts(s1);
    puts(s2);
    d=stricmp(s1,s2);	
    printf("ASCII Value difference: %d",d);
    getch();
    return 0;	
}
Output:

stricmp() function in C

strstr()

By using this predefined function, we can find the substring of a string. The strstr() function requires 2 arguments of type const char* and returns char*.

Syntax: char*strstr (const char*str, const char*sub);

If the searching substring is available, then strstr() returns the base address of the substring else returns null.

Program: To understand strstr() function in C
#include <stdio.h>
#include<conio.h> 
#include<string.h>
int main()
{
    char s1[30]="HelloHiWelcome";
    char s2[30]="Hi";
    char *ptr;
    ptr=strstr(s1,s2);	
    if (ptr)
    {
       printf("String found\n"); 
       printf("First occurrence of string '%s' in '%s' is '%s'", s2, s1, ptr);
    }
    else
    {
       printf("\nsunstring not found");
    }
    getch();
    return 0;	
}
Output:

String PreDefined Functions in C Language with Examples

In the next article, I am going to discuss How to Find the Length of a String in C Language with Examples. Here, in this article, I try to explain String PreDefined Functions in C Language with Examples. I hope you enjoy this String PreDefined Functions in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this String PreDefined Functions in C Language Language with Examples article

Leave a Reply

Your email address will not be published.