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.
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 <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.
Output:
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:
Output:
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.
Output:
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:
Output:
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.
Output:
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 <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:
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.