C Programming

Use of strlen, strrev, strlwr and strupr()

String is a collection of character elements which behaves like a normal array. In C language, there are many types of different standard functions by which we can operate different types of strings. It means these standard functions help the programmer to manipulate different types of strings easily. In today’s topic we will discuss different types of string function like strlen (), strrev (), strlwr () and strupr () etc.

Strlen ():

Strlen () function is used to calculate the length of the string. It takes a string as an argument inside its parenthesis and gets the result of the length of the string.

Example-1:

In this programming example, we will see how strlen () function calculates the length of the string.

#include<stdio.h>

#include<string.h>

int main ()
{
    char s[100] ;   // declaration of string.
    int a ;
    printf ( " enter a string \n " ) ;
    gets(s);        // a string is given by the user.
    a = strlen ( s ) ;  // application of strlen () function.
    printf ( " Total words in the string = %d \n ",a ) ;
    return 0 ;
}

Output:

Explanation:

Here, we declared a string s [] and the value of this string is given by the user with the help of gets () function. Now, we call the strlen () function and inside its parenthesis we will pass the string. Strlen () function counts the total character present in the string and this value is initialized to the variable a. Now, we print the value of the variable a to see the counting result of strlen () function.

Example-2:

Here, we will see another example of strlen () function:

#include <stdio.h>

#include <string.h>

int main ()
{
    char x[20] = "Program" ;
    char y[20] = {'P','r','o','g','r','a','m','\0'} ;
 
    // using the %zu format specifier to print size_t
    printf ( "  Length of string x = %zu \n ", strlen(x) ) ;
    printf ( " Length of string y = %zu \n ", strlen(y) ) ;     // applying strlen () function.
 
    return 0 ;
}

Output:

Explanation:

Here, we declared two strings x [] and y [] and the value of this string is already given in the program. Now we call the strlen () function and inside its parenthesis we will pass the strings. Strlen () function counts the total characters present in the strings and now we print these values.

Strrev ():

Strrev () function is used to reverse the string. It takes a string as an argument inside its parenthesis and get the result of the reverse of the string.

Example-3:

In this programming example we will see an example of strrev () function and how strrev () function reverse the string.

#include<stdio.h>

#include<string.h>

int main ()
{
    char s[100] ;       // declaring a string.
    printf ( " enter a string \n " ) ;
    gets(s);
    printf ( " Reverse of the string = %s \n ",strrev(s) ) ;    // using strrev () function.
    return 0 ;
}

Output:

enter a string

hello

Reverse of the string = olleh

Explanation:

Here, we declared the string s [] and the value of this string is given by the user with the help of gets () function. Now, we call the strrev () function and inside its parenthesis, we will pass the string. Strrev() function reverses the string and shows the result of reversed string in the monitor with the help of printf () function.

Example-4:

Here we will see another example of strrev () function:

#include<stdio.h>

#include<string.h>

int main ()
{
   char n [ 30 ] = " Hello " ;  // declaration and initialization of a string.

   printf ( " String before strrev( ) : %s \n " , n ) ;

   printf ( " String after strrev( )  : %s \n " , strrev ( n ) ) ;  // strrev () function is calling.

   return 0 ;
}

Output:

String before strrev( ) : Hello

String after strrev( ) : olleH

Explanation:

Here, we declared a string n [] and the value of this string is already given in the program. The string is “Hello”. Now, we call the strrev () function and inside its parenthesis we will pass the string. Strrev () function reverses the string gets the string “olleH” and shows the result of reversed string in the monitor with the help of printf () function.

Strupr ():

Strupr () function gets a string and turn its every letter to capital letters.

Example-5:

In this programming example, we will see an example of strupr () function and how strupr () function turns all the letter of a string to capital letters.

#include<stdio.h>

#include<string.h>

int main ()
{
   char n [ 30 ] ;  // a string is declared.

   printf ( " Enter a name in lower case \n "  ) ;
   gets(n);
   printf ( " Entered name in upper case  %s \n " , strupr ( n ) ) ;    // application of strupr () function.

   return 0 ;
}

Output:

Enter a name in lower case

hello

Entered name in upper case HELLO

Explanation:

Here, we declared a string n [] and the value of this string is already given in the program. The string is “hello”. All the letters are in small caps. Now, we call the strupr () function and inside its parenthesis we will pass the string. Strupr() function turns all the letter of the string to capital letters.

Example-6:

Here we will see another example of strupr () function:

#include<stdio.h>

#include <string.h>

int main()
{
  char s [] = " hello user " ;  // declare a string.

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

printf ( " \n string after converting to the uppercase is : %s \n " , strupr ( s ) ) ;  // strupr () function is called.
  return 0 ;
}

Output:

Given string is: hello user

string after converting to the uppercase is: HELLO USER

Explanation:

Here, we declared a string s [] and the value of this string is already given in the program. The string is “hello user”. All the letters are in small caps. Now, we call the strupr () function and inside its parenthesis we will pass the string. Strupr () function turns all the letter of the string to capital letters.

Strlwr ():

Strupr () function gets a string and turn its every letter to small caps.

Example-7:

In this programming example, we will see an example of strlwr () function and how strlwr () function turns all the letter of a string to small caps.

#include<stdio.h>

#include<string.h>

int main ()
{
   char n [ 30 ] ;  // declare a string.

   printf ( " Enter a name in upper case \n "  ) ;
   gets(n);
   printf ( " Entered name in lower case  %s \n " , strlwr ( n ) ) ;    // strupr () function is called.

   return 0 ;
}

Output:

Enter a name in upper case

HELLO

Entered name in lower case “hello”.

Explanation:

Here, we declared a string n [] and the value of this string is already given in the program. The string is “HELLO”. All the letters are in capital letter. Now, we call the strlwr () function and inside its parenthesis we will pass the string. Strlwr () function turns all the letter of the string to small caps.

Example-8:

Here we will see another example of strlwr () function.

#include<stdio.h>

#include <string.h>

int main()
{
  char s [] = " HELLO USER " ;      // declaration and initialization of a string.

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

printf ( " \n string after converting to the lowercase is : %s \n " , strlwr ( s ) ) ;  // strupr () function is called
  return 0 ;
}

Output:

Given string is : HELLO USER

string after converting to the lowercase is: hello user

Explanation:

Here, we declared a string named s [] and the value of this string is already given in the program. The string is “HELLO USER”. All the letters are in capital letter. Now we call the strlwr () function and inside its parenthesis we will pass the string. Strlwr () function turns all the letter of the string to small caps.

Conclusion:

This discussed the implementation of different types of functions. With the help of these function, we can perform different types of operations on strings. It helps the programmer to make the programming code length small and reduce the complexity of the program.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com