String Handling functions in C
String :-
The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any character in the double quotation, we can say it is a string. To declare a string, we use the char data type.
Ex :-
#include <stdio.h>
int main()
{
char s[50]="JavaTpoint"; // string declaration
printf("The given string is:");
printf("%s",s);// printing the string
return 0;
}
Output:-
The given string is: JavaTpoint
String handling functions in c:-
It is also known as string manipulation. To reduce the programmer's time and to make the code more effective in c, we have some in-build functions in that we have string handling functions.
The String handling function is used to perform some operations on strings. All the string handling functions are embedded in string.h library. we can perform String handling functions, By including string.h header file
S.no | String function | Syntax | Description |
01 . | strlen() | str( String_name) | It returns the length of the given string |
02 . | strrev() | strrev(String_name) | It reverse the given string and returns to the main function. |
03 . | strcpy() | strcpy (string1, string2) | With this function the character in the string2 are copied in the string1 |
04 . | strncpy() | strcpy(string1, string2, n) | With this the first n characters of string2 are copied in the string1 |
05 . | strcat() | strcat (string1, string2) | It is used to append the characters of string2 into string1 |
06 . | strlwr() | strlwr (string1) | All the characters in the string1 are converted into lower case letters |
07 . | strupr() | strupr (string1) | All the characters in the string1 are converted into upper case letters |
08 . | strcmp() | strcmp (string1, string2) | It compares string1 and string2 if both the strings are same then it returns 0, if string1>string2, it returns ASCII value greater than 0,if string1<string2, it returns ASCII value less than 0. |
09 . | strncmp() | strncmp(string1, string2, n) | It is used to compare the first n characters of string1 and string2 . |
10 . | strcmpi() | strcmp(string1, string2) | It is same as the strcmp() function but the difference is it is not case sensitive .ie,”A” and “a” are treated as same. |
11 . | strncat() | strncat(string1, string2,n) | It is used to append the first n characters of string2 in string1. |
12 . | strchr() | strchr(string1, ’a’) | The function of this function is to return the pointer to the first occurrence of the character ‘A’ in the string1 . |
13 . | strrchr() | strchrr(string1 , ’b’) | The function of this function is to return the pointer to the last occurrence of the character ‘A’ in the string1 . |
14 . | strstr() | strstr(string1, string2) | The function of this function is to return the pointer to the last occurrence of the string2 in the string1 . |
15 . | strset() | Strset( string , ’A’) | By this functions all the characters in the string1 is converted to ‘A’ . |
16 . | strnset() | strnset(string1 ,’B’ ,n) | This function sets the first n characters as ‘B’ . |
17 . | strdup() | string2=strdup(string1) | The duplicate characters of string1 are stored in string2 . |
Examples:-
//program for strlen()
#include<stdio.h>
#include<string.h>
int main()
{
char s[100] = “JavaTpoint”;
// printing the length of the string
printf(“The length of the given string is :%d”,strlen(s));
return 0;
}
Output :
The length of the given string is :10
//program for strrev()
#include <stdio.h>
#include <string.h>
int main()
{
char s[100] = "JavaTpoint";
// printing the length of the string
printf("The reverse of the given string is: is %s",strrev(s));
return 0;
}
Output :
the reverse of the given string is :tniopTavaJ
//Program for strcmp(),strncmp()
#include<stdio.h>
#include<string.h>
int main()
{
char s[50],a[50];
printf("\nEnter the first string:");
scanf("%s",s);
printf("\nEnter the second string:");
scanf("%s",a);
//comparing both the strings
printf("%d %d",strcmp(s,a),strncmp(s,a,5));
}
Output 1 :
Enter the first string: sophie
Enter the second string: graner
0 0
// program for strcpy() strncpy()
#include<stdio.h>
#include<string.h>
int main()
{
char s[50]=" ",a[50];
printf ("Enter the second string:\n");
scanf("%s" ,a);
//comparing both the strings
printf ("The first string is :%s\n",strncpy(s,a,3));
printf ("The first string is:%s",strcpy(s,a));
return 0 ;
}
Output :
Enter the second string: JavaTpoint
The first string is : Jav
The first string is : JavaTpoint
// program for strset() strnset(
#include <stdio.h>
#include<string.h>
int main() {
char s[100]="JavaTpoint";
printf ("Before strset() function: %s",s);
// setting all the characters as A
printf("\nstring after strset() function:%s",strset(s,A'));
// setting first five charecters as B
printf("\n string after strnset() function %s",strset(s,5,'B'));
return 0 ;
}
Output :-
Before strset() function: JavaTpoint
After strset() function: AAAAAAAAAA
Atrer strnset() function: AAAAApoint
//program for strcat() and strncat()
include <stdio.h>
#include<string.h>
int main() {
char s1[100]=" ";
char s2[100]="JavaTpoint";
printf ("Before strcat() function:\n s1=%s\n s2=%s",s1,s2);
// Appending first 5 characters of s2 into s1
strset(s1,s2,5);
printf (" After strncat() function:\n s1=%s\n s2=%s",s1,s2);
// Appending all characters of s2 into s1
strcat(s1,s2);
printf (" After strcat() function:\n s1=%s\n s2=%s",s1,s2);
return 0 ;
}
Output :-
Before strcat() function:
S1=
S2 =JavaTpoint
After strncat() function :
S1=JavaT
S2=JavaTpoint
After strcat() function :
S1=JavaTpoint
S2=JavaTpoint
// program for strupr() ans strler()
#include<stdio.h>
#include<string.h>
Int main()
{
char s[100] = ”Javatpoint”;
printf(“string before strupr() function :\ns=%s”, s);
// printing upper case string
printf(“string After strupr() function :\ns=%s”, s);
// printing lower case string
printf(“string After strlwr() function :\ns=%s”, s);
return 0;
}
Output :-
String before strupr :
s= JavaTpoint
string After strupr() function :
s= JAVATPOINT
string After strlwr() function :
s= javatpoint
Applications of String :-
1 .Information Retrieval :-
String handling functions can be used to retrieve data from an unknown source.
2 .plagiarism detector :-
By using strings, the computer can identify the percentage of the content, code matching with the content That already exists in the database or websites.
3. Cipher Text Generator:-
The transformation of data from one source to another source without any interruption and any third-party attack or loss of data can be done by the strings.
4 .Spam detection :-
Currently, if we open Gmail, we have a separate section called spam. In that, we have unimportant mails, advertisement mails ,spam mails this mails are identified by the help of strings.
5. Search Engines:-
If we search anything in the search engine, it recommends the data and websites that are close to the content which we have searched. The search algorithm compares the given String to the strings which are present in the database and displays the result.
6. Bioinformatics :-
In the field of bioinformatics, to match the DNA sequences, string matching modules .it are also used to find the DNA patterns and genetic sequences.
Disadvantages of strings:-
1. In c, the strings are static. The size of the strings is defined at the beginning of the program. So we can't perform strings dynamically in c.
2 . The string performance is slow while performing operations like input and output.
3 . The String is immutable in java. We can't change the data which is present in the String.