What are the different types of string functions in C ?
C provides a set of string handling functions that can be used to manipulate strings. These functions are defined in the string.h library. Some of the commonly used string handling functions in C language are:
- strcpy(destination, source) : String Copy Function
- strcat(destination, source) : String Concatenation Function
- strrev( ) : String Reverse Function
- strlen( ) : String Length Function
- strcmp( ) : String Compare Function
- strstr(destination, source): Search Substring
- strlwr( ) : String Lowecase
- strupr( ) : String Uppercase
1. strcpy(destination, source) : String Copy Function
This function copies the string pointed to by "source" to the memory location pointed to by "destination". The destination string must be large enough to hold the source string. The strcpy()
function in C is used to copy the contents of one string to another. The function takes two parameters: the destination string (to which the source string will be copied) and the source string.
Here's an example of how strcpy()
can be used:
#include <string.h>
#include <stdio.h>
int main()
{
char str1[20] = "Hello";
char str2[20];
strcpy(str2, str1);
printf("str2: %s", str2);
return 0;
}
This program will copy the string "Hello" from the str1 to str2, and will print the str2 "Hello"
2. strcat(destination, source) : String Concatenation Function
This function concatenates the string pointed to by "source" to the end of the string pointed to by "destination". The destination string must be large enough to hold the concatenated string. The strcat()
function in C is used to concatenate (or join) two strings together. The function takes two parameters: the destination string (to which the source string will be appended) and the source string.
Here's an example of how strcat()
can be used:
#include <stdio.h>
#include <string.h>
int main()
{
char dest[20] = "Hello, ";
char src[] = "world!";
strcat(dest, src);
printf("%s", dest); // Output: Hello, world!
return 0;
}
In this example, the string "Hello, " is assigned to the dest
array, and the string "world!" is assigned to the src
array. The strcat()
function is then used to concatenate the two strings together, and the resulting string "Hello, world!" is printed to the console.
It is important to note that the destination string should have enough space to hold the concatenated string, otherwise it will cause buffer overflow.
3. strrev( ) : String Reverse Function
The strrev()
function in C is used to reverse the order of characters in a string. This function is not a part of the standard C library, it is a non-standard library function that is provided by some compilers. The function takes a single parameter: the string that you want to reverse.
Here's an example of how strrev()
can be used:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "CPROGRAM";
printf("Original String: %s", str);
strrev(str);
printf("Reversed String: %s", str);
return 0;
}
OUTPUT: MARGORPC
4. strlen() : String Length Function
In C, the strlen()
function is used to determine the length of a string. The function takes a single argument, which is a pointer to the string, and it returns an integer value representing the number of characters in the string, not including the null character ('�') that terminates the string.
Here's an example of how strlen()
can be used:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "Hello, World!";
int len;
len = strlen(str);
printf("Length of the string "%s" is %d.", str, len);
return 0;
}
OUTPUT: Length of the string "Hello, World!" is 13.
5. strcmp( ) : String Compare Function
In C, the strcmp()
function is used to compare two strings. The function takes two arguments: the first is a pointer to the first string, and the second is a pointer to the second string.
The function returns an integer value:
- 0 if the strings are identical
- a value less than 0 if the first string is lexicographically less than the second string
- a value greater than 0 if the first string is lexicographically greater than the second string
Here's an example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "Hello";
char str2[100] = "world";
int cmp;
cmp = strcmp(str1, str2);
if (cmp == 0)
{
printf(""%s" and "%s" are identical.", str1, str2);
} else if (cmp < 0)
{
printf(""%s" is lexicographically less than "%s".", str1, str2);
}
else
{
printf(""%s" is lexicographically greater than "%s".", str1, str2);
}
return 0;
}
In this example, the strcmp()
function is used to compare the strings "Hello" and "world". Since "Hello" comes lexicographically before "world", the function returns a value less than 0.
OUTPUT: "Hello" is lexicographically less than "world".
6. strstr(destination, source): Search Substring
In C, the strstr()
function is used to search for a substring within a string. The function takes two arguments: the first is a pointer to the string to be searched, and the second is a pointer to the substring to search for.
The function returns a pointer to the first occurrence of the substring within the string, or a null pointer if the substring is not found.
Here's an example:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "Hello, world!";
char sub[100] = "world";
char *found;
found = strstr(str, sub);
if (found)
{
printf(""%s" found in "%s" at position %ld", sub, str, found - str);
}
else
{
printf(""%s" not found in "%s", sub, str);
}
return 0;
}
OUTPUT: "world" found in "Hello, world!" at position 7
In this example, the strstr()
function is used to search for the substring "world" within the string "Hello, world!". The function returns a pointer to the first occurrence of the substring within the string, which is at position 7. The position can be calculated by subtracting the pointer of the main string from the pointer of found substring.
7. strlwr( ) : String Lowercase
In C, the strlwr()
function is used to convert all the uppercase characters in a string to lowercase. This function is not a standard C library function and it is specific to some compilers and operating systems like Windows.
The function takes a single argument, which is a pointer to the string, and it returns a pointer to the same string with all uppercase characters converted to lowercase.
Here's an example:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "HeLLo, WorLD!";
printf("Original String : %s", str);
printf("Lowercase String : %s", strlwr(str));
return 0;
}
OUTPUT:
Original String : HeLLo, WorLD!
Lowercase String : hello, world!
In this example, the _strlwr()
function is used to convert all uppercase characters in the string "HeLLo, WorLD!" to lowercase, resulting in the string "hello, world!".
8. strupr( ) : String Uppercase
In C, the strupr()
function is used to convert all the lowercase characters in a string to uppercase. This function is not a standard C library function and it is specific to some compilers and operating systems like Windows.
The function takes a single argument.
Here's an example:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "HeLLo, WorLD!";
printf("Original String : %s", str);
printf("Uppercase String : %s", strupr(str));
return 0;
}
In this example, the _strupr()
function is used to convert all lowercase characters in the string "HeLLo, WorLD!" to uppercase, resulting in the string "HELLO, WORLD!".
OUTPUT:
Original String : HeLLo, WorLD!
Uppercase String : HELLO, WORLD!