string is a sequence of characters which is terminated with a null character \0.
For example:
- char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.
How to declare a string?
Here's how you can declare strings:
- char s[5];
Here, we have declared a string of 5 characters.
How to Read String from the user
You can use the scanf() function to read a string. The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab etc.).
Example 1: scanf() to read a string
- #include <stdio.h>
- int main()
- {
- char name[20];
- printf("Enter name: ");
- scanf("%s", name);
- printf("Your name is %s.", name);
- return 0;
- }
Output
Enter name: Csdt Centre
Your name is Csdt.
Even though Csdt Centre was entered in the above program, only “Csdt” was stored in the name string. It's because there was a space after Csdt.
How to read a line of text?
You can use the fgets() function to read a line of string. And, you can use puts() to display the string.
Example 2: fgets() and puts()
- #include <stdio.h>
- int main()
- {
- char name[30];
- printf("Enter name: ");
- fgets(name, sizeof(name), stdin); // read string
- printf("Name: ");
- puts(name); // display string
- return 0;
- }
Output
Enter name: Csdt Centre
Name: Csdt Centre
Note: The gets() function can also be to take input from the user. However, it is removed from the C standard.
It's because gets() allows you to input any length of characters. Hence, there might be a buffer overflow.
String Predefine Function
- strlen()
The strlen() function takes a string as an argument and returns its length. The returned value is of type long int.
It is defined in the <string.h> header file.
Example: C strlen() function
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char a[20]="Program";
- char b[20]={'P','r','o','g','r','a','m','\0'};
- printf("Length of string a = %ld \n",strlen(a));
- printf("Length of string b = %ld \n",strlen(b));
- return 0;
- }
Output
Length of string a = 7
Length of string b = 7
Note that the strlen()
function doesn't count the null character \0
while calculating the length.
2. strcat() function:
strcat() concatenates (joins) two strings.
The strcat()
function is defined in <string.h> header file. It takes two arguments, i.e, two strings or character arrays, and stores the resultant concatenated string in the first string specified in the argument.
Example: C strcat() function
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char str1[] = "This is ", str2[] = “CSDT”;
- //concatenates str1 and str2 and resultant string is stored in str1.
- strcat(str1,str2);
- puts(str1);
- puts(str2);
- return 0;
- }
Output
This is CSDT
CSDT
3. strcmp() function:-
The strcmp() function takes two strings and returns an integer. It is used to compares two strings character by character.
If the first character of two strings is equal, the next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '\0' is reached. It is defined in the string.h header file.
Return Value from strcmp()
Return Value |
Remarks |
0 |
if both strings are identical (equal) |
negative |
if the ASCII value of the first unmatched character is less than second. |
positive integer |
if the ASCII value of the first unmatched character is greater than second. |
Example: C strcmp() function
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
- int result;
- // comparing strings str1 and str2
- result = strcmp(str1, str2);
- printf("strcmp(str1, str2) = %d\n", result);
- // comparing strings str1 and str3
- result = strcmp(str1, str3);
- printf("strcmp(str1, str3) = %d\n", result);
- return 0;
- }
Output
strcmp(str1, str2) = 32
strcmp(str1, str3) = 0
The first unmatched character between string str1 and str2 is third character. The ASCII value of 'c' is 99 and the ASCII value of 'C' is 67. Hence, when strings str1 and str2 are compared, the return value is 32.
When strings str1 and str3 are compared, the result is 0 because both strings are identical.
4.strcpy() Function: The strcpy() function copies the string pointed by source (including the null character) to the character array destination. The function also returns the copied array.
The strcpy() function is defined in the string.h header file.
Example: C strcpy()
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char str1[10]= "awesome";
- char str2[10];
- char str3[10];
- strcpy(str2, str1);
- strcpy(str3, "well");
- puts(str2);
- puts(str3);
- return 0;
- }
Output
awesome
well
Note:- It is important to note that the destination array should be large enough to copy the array. Otherwise, it may result in undefined behavior.