C programming language provides a set of pre-defined functions called string handling functions to work with string values. The string handling functions are defined in a header file called string.h. Whenever we want to use any string handling function we must include the header file called string.h.
The following table provides most commonly used string handling function and their use…
Function | Syntax (or) Example | Description |
---|---|---|
strcpy() | strcpy(string1, string2) | Copies string2 value into string1 |
strncpy() | strncpy(string1, string2, 5) | Copies first 5 characters string2 into string1 |
strlen() | strlen(string1) | returns total number of characters in string1 |
strcat() | strcat(string1,string2) | Appends string2 to string1 |
strncat() | strncpy(string1, string2, 4) | Appends first 4 characters of string2 to string1 |
strcmp() | strcmp(string1, string2) | Returns 0 if string1 and string2 are the same; less than 0 if string1<string2; greater than 0 if string1>string2 |
strncmp() | strncmp(string1, string2, 4) | Compares first 4 characters of both string1 and string2 |
strcmpi() | strcmpi(string1,string2) | Compares two strings, string1 and string2 by ignoring case (upper or lower) |
stricmp() | stricmp(string1, string2) | Compares two strings, string1 and string2 by ignoring case (similar to strcmpi()) |
strlwr() | strlwr(string1) | Converts all the characters of string1 to lower case. |
strupr() | strupr(string1) | Converts all the characters of string1 to upper case. |
strdup() | string1 = strdup(string2) | Duplicated value of string2 is assigned to string1 |
strchr() | strchr(string1, ‘b’) | Returns a pointer to the first occurrence of character ‘b’ in string1 |
strrchr() | ‘strrchr(string1, ‘b’) | Returns a pointer to the last occurrence of character ‘b’ in string1 |
strstr() | strstr(string1, string2) | Returns a pointer to the first occurrence of string2 in string1 |
strset() | strset(string1, ‘B’) | Sets all the characters of string1 to given character ‘B’. |
strnset() | strnset(string1, ‘B’, 5) | Sets first 5 characters of string1 to given character ‘B’. |
strrev() | strrev(string1) | It reverses the value of string1 |
#include<stdio.h> #include<string.h> int main() { char name[50],name1[50],name2[50],ch='B'; char *x; //pointer printf("Enter 1st string: "); gets(name);
printf("Enter 2nd string: "); gets(name1);//input second string printf("strlen() function: %d\n",strlen(name)); printf("strcat() function: %s\n",strcat(name,name1)); strcpy(name2,name); printf("strcpy() function: %s\n",name2); printf("strcmp() function: %d\n",strcmp(name,name1)); printf("strlwr() function: %s\n",strlwr(name)); printf("strupr() function: %s\n",strupr(name)); x=strchr(name,ch); printf("strchr()function. The string after ch is: %s\n",x); x=strstr(name,name1); printf("strstr() function: %s",x); return 0; }
Output:- Enter 1st string: The bell rings Enter 2nd string: bell strlen() function: 14 strcat() function: The bell ringsbell strcpy() function: The bell ringsbell strcmp() function: -1 strlwr() function: the bell ringsbell strupr() function: THE BELL RINGSBELL strchr()function. The string after ch is: BELL RINGSBELL strstr() function:
Other important library functions are:
- strncmp(str1, str2, n) :it returns 0 if the first n characters of str1 is equal to the first n characters of str2, less than 0 if str1 < str2, and greater than 0 if str1 > str2.
- strncpy(str1, str2, n) This function is used to copy a string from another string. Copies the first n characters of str2 to str1
- strchr(str1, c): it returns a pointer to the first occurrence of char c in str1, or NULL if character not found.
- strrchr(str1, c): it searches str1 in reverse and returns a pointer to the position of char c in str1, or NULL if character not found.
- strstr(str1, str2): it returns a pointer to the first occurrence of str2 in str1, or NULL if str2 not found.
- strncat(str1, str2, n) Appends (concatenates) first n characters of str2 to the end of str1 and returns a pointer to str1.
- strlwr() :to convert string to lower case
- strupr() :to convert string to upper case
- strrev() : to reverse string
Converting a String to a Number
In C programming, we can convert a string of numeric characters to a numeric value to prevent a run-time error. The stdio.h library contains the following functions for converting a string to a number:
- int atoi(str) Stands for ASCII to integer; it converts str to the equivalent int value. 0 is returned if the first character is not a number or no numbers are encountered.
- double atof(str) Stands for ASCII to float, it converts str to the equivalent double value. 0.0 is returned if the first character is not a number or no numbers are encountered.
- long int atol(str) Stands for ASCII to long int, Converts str to the equivalent long integer value. 0 is returned if the first character is not a number or no numbers are encountered.
The following program demonstrates atoi() function:
#include <stdio.h> int main() {char *string_id[10]; int ID; printf("Enter a number: "); gets(string_id); ID = atoi(string_id); printf("you enter %d ",ID); return 0;}
Output:
Enter a number: 221348 you enter 221348
- A string pointer declaration such as char *string = “language” is a constant and cannot be modified.
Examples of String Functions in C
String function is easy to use. Here we will discuss how to use string function in C programming with the help of examples
1. Printf()
This function is used to print the string which is present inside the double quotes (“”) of this function. It can also be used to concatenate two strings.
Code:
#include<stdio.h>
int main()
{
printf("Name: Hardik");
}
2. gets()
This function is used to get the input string from the user.
Code:
#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
}
3. puts()
This is the C language based function which is used to display the string on the console screen.
This is different from the printf() function in the regard that puts() writes the string s and a newline to stdout i.e. it is only used to display the strings whereas the printf() is used to display all kinds of outputs to stdout.
Code:
#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
puts(name);
}
4. char
This function in the below example means that the string of s is declared with a size of 5 with the character data type. This comes under the declaration and definition part of the string.
Code:
char s[5]
5. scanf()
This is another string function popularly used in C language. It is used to read the characters till the time whitespace such as space, tab or a newline is encountered. In essence, this is used to read a string.
Code:
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
}
6.strcpy(s1, s2)
This function is used to copy the contents of the string s2 into the primary string s1.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
}
7. strcat()
This function is used to concatenate the contents of string s2 after string s1.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
strcat( str1, str2);
}
8. Strlen()
As the name suggests, this function is used to calculate the length of the string.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
}
9. strrev()
This function is used to return the reversal of the string provided.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
}
10. strcmp()
This function is used to compare the two strings and return 0 if both the strings are equal.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str1[10],str2[10];
gets(str1);
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings :equal");
else
printf("Strings: not equal");
}
11. strupr()
This function is used to return the characters in the upper case.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
gets(str);
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
}
12. Strlwr()
As the name suggests, this function is used to return the characters of the string in the lower case.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
gets(str);
printf("String is: %s",str);
printf("\nUpper String is: %s",strlwr(str));
}
13. sizeof()
This function is used to return the size of the string i.e. the characters the string is capable to hold at a stretch.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Shishir";
sizeof(str1)
}
14. strchr()
This function searches for the character in a particular string.
Code:
#include <stdio.h>
#include <string.h>
int main () {
const char str[] = "http://www.google.com";
const char ch = '.';
char *ret;
ret = strchr(str, ch);
printf("String after |%c| is - |%s|\n", ch, ret);
}
The output for the above code snippet is:
Output: String after |.| is – |.google.com|
15. strstr()
This function is used to search for a given string inside an already present string.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char inputstr[70] = "String Function in C at Shishir";
printf ("Output string is: %s", strstr(inputstr, 'Shi'));
}
The output for the above code snippet would be:
Output: Shishir