gets()
, puts()
, etc. strlen()
Besides, you will learn to perform various operations on strings by accepting strings from the user.Library Functions - String Manipulation
Table of Contents
Depending on the type of problem you may need to use strings more often. If you want to perform exact string manipulation manually, it can be very complicated and you may run into problems most of the time.
So we recommend you to use C library functions to overcome this problem.
Standard Library Functions:"string.h"
There are numerous functions for manipulating strings under.
Commonly used functions for manipulating strings and their functions are given below:
Function | Function Work |
---|---|
strlen() | Determines the length of the string. |
strcpy() | Copy a string to another string |
strach() | Concatenates two strings |
string() | Converts string to lowercasse. |
strcpy() | Copy a string to another string |
strupr() | Converts string to uppercase. |
strrev() | Sorts strings in reverse order. |
strstr() | Finds the substring from a string. |
String handling functions "string.h"
are defined under the header file.
So add the following code to run the string handling functions in your program.
#include
gets() and puts()
In C programming, library functions gets()
and puts()
are used for receiving and displaying strings respectively.
#include
int main()
{
char name[30];
printf("Enter name: ");
get(name); //Function to receive string from user
printf("Name: ");
puts(name); //String display function
return 0;
}
Note:gets()
Both puts()
functions for handling strings "stdio.h"
are defined in the header file.
C strlen() function
strlen()
The function returns the length of a string. It does not count the null character (‘\0’).
Example: The following example strlen()
shows the length of the string ch[30] using the function:
#include
int main()
{
char ch[30]={'s', 'a', 't', 't', 'a', 'c', 'a', 'd', 'e', 'm', 'y', '\0'};
printf("The length of string is: %d",strlen(ch));
}
output
The length of string is: 11
C strcpy() function
strcpy(destination, source)
The function copies the string from the source string to the destination string.
Example: The following example shows how strcpy() function copies string ch1[30] into string ch2[30]:
#include
int main()
{
char ch1[30]={'s', 'a', 't', 't', 'a', 'c', 'a', 'd', 'e', 'm', 'y', '\0'};
char ch2[30];
strcpy(ch2,ch1);
printf("The value of second string is: %s",ch2);
}
output
The value of second string is: sattacademy
C strcat() function
strcat(first_string, second_string)
The function concatenates two strings together and returns the first string.
#include
int main()
{
char ch1[10] = {'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10] = {'s', 'a', 't', 't', '\0'};
strcat(ch1,ch2);
printf("The value of first string is: %s",ch1);
}
output
The value of first string is: hellosatt
C strcmp() function
strcmp(first_string, second_string)
The function compares two strings and returns 0 (zero) if both strings are equal.
Here we use gets() function to read string from console.
#include
int main()
{
char str1[30],str2[30];
printf("Enter first string: ");
gets(str1); //Read string from console
printf("Enter second string: ");
get(str2);
if(strcmp(str1,str2)==0)
printf("Two strings are equal");
else
printf("Two strings are not equal");
}
output
Enter first string: set
Enter second string: academy
Two strings are not equal
C strlwr() function
strlwr(string)
The function converts a string to lowercase.
#include
#include
int main(){
char str[30];
clrscr();
printf("Enter string: ");
gets(str);//Reads string from console.
printf("String is: %s",str);
printf("\nLower String is: %s",strlwr(str));
getch();
}
output
Enter string: sattacademy
String is: sattacademy
Upper String is: SATTACADEMY
C strrev() function
strrev(string)
The function reverses a string. That is, the first characters of the string come last and the last characters first.
#include
#include
int main(){
char str[30];
clrscr();
printf("Enter string: ");
gets(str);//Reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
getch();
}
output
Enter string: sattacademy
String is: sattacademy
Reverse String is: ymedacattas
C strstr() function
The strstr() function returns a pointer to the first occurrence of the matching string in the given string. It returns the matching first to last character of substring.
Syntax:
char *strstr(const char *string, const char *match)
Explanation of syntax:
string: This refers to the entire part of the string. from which the substring will be parsed.
match: This is the submatch that will be sorted from the entire string.
#include
#include
#include
int main(){
char str[100]="I love programming with C";
char *sub;
clrscr();
sub=strstr(str,"programming");
printf("\nSubstring is: %s",sub);
getch();
}
output
Substring is: programming with C