CSDT BLOG

DISCOVER COLLECTIONS AND BLOGS THAT MATCH YOUR INTERESTS.




Share ⇓




String in C Language

Bookmark

String in C Language

string is a sequence of characters which is terminated with a null character \0.


For example:

  1. 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.

c-string.jpg

How to declare a string?

Here's how you can declare strings:

  1. char s[5];

c-string-declaration_0.jpg

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


  1. #include <stdio.h>
  2. int main()
  3. {
  4.     char name[20];
  5.     printf("Enter name: ");
  6.     scanf("%s", name);
  7.     printf("Your name is %s.", name);
  8.     return 0;
  9. }

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()

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     char name[30];
  5.     printf("Enter name: ");
  6.     fgets(name, sizeof(name), stdin);  // read string
  7.     printf("Name: ");
  8.     puts(name);    // display string
  9.     return 0;
  10. }

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

  1. 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

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     char a[20]="Program";
  6.     char b[20]={'P','r','o','g','r','a','m','\0'};
  7.     printf("Length of string a = %ld \n",strlen(a));
  8.     printf("Length of string b = %ld \n",strlen(b));
  9.     return 0;
  10. }

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

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     char str1[] = "This is ", str2[] = “CSDT”;
  6.     //concatenates str1 and str2 and resultant string is stored in str1.
  7.     strcat(str1,str2);
  8.     puts(str1);    
  9.     puts(str2); 
  10.     return 0;
  11. }

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

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
  6.     int result;
  7.     // comparing strings str1 and str2
  8.     result = strcmp(str1, str2);
  9.     printf("strcmp(str1, str2) = %d\n", result);
  10.     // comparing strings str1 and str3
  11.     result = strcmp(str1, str3);
  12.     printf("strcmp(str1, str3) = %d\n", result);
  13.     return 0;
  14. }

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()

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     char str1[10]= "awesome";
  6.     char str2[10];
  7.     char str3[10];
  8.     strcpy(str2, str1);
  9.     strcpy(str3, "well");
  10.     puts(str2);
  11.     puts(str3);
  12.     return 0;
  13. }


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.




5

Our Recent Coment