Strings in C programming are an array of characters with a NULL character ('') appended at the end index. Unlike character arrays, a c-string is a sequence of characters that are treated by the compiler as a single item. But just like an array, a string can be traversed along with its indices.

1. Declaration of Strings in C

Declaration of a String in C is exactly the same as in the case of an Array of characters.

char String_Name[No.of.elements];

Note: here the number of elements should include the NULL character ('') too.

2. The Initialization of Strings in C

A string in C could be initialized in different ways. When creating a string by specifying individual characters like example 2 and 4 below, you also make sure that the NULL character is added to it.

  1. char string[20]=”JournalDev”;
  2. char string[20]= { ‘J’ , ‘o’, ‘u’ , ‘r’ , ‘n’ , ‘a’ , ‘l’ , ‘D’ , ‘e’ , ‘v’ , ‘’ };
  3. char string[]=”JournalDev”;
  4. char string[]= { ‘J’ , ‘o’, ‘u’ , ‘r’ , ‘n’ , ‘a’ , ‘l’ , ‘D’ , ‘e’ , ‘v’ , ‘’ };

For both 3rd and 4th cases the number of elements is not initially mentioned. In these situations, the size of the array is by-default set to the total number of elements(including '').

3. Traversing a String in C

For example, let us take a look at how a string can be traversed similar to an array.


4. Reading String in C (using scanf() gets() or fgets())

Both scanf() or gets() functions are useful for reading a string from the user. But, unfortunately, the scanf() function only reads a sequence of characters until it encounters a newline, space, or tab, etc. Whereas, gets() reads the sequence of characters until a newline(or enter key). Further, fgets() is used to avoid the buffer overflow possible due to gets(). Let’s take a look at how all the above-mentioned functions work.

For example,


Input using scanf()

As you can see, due to the presence of whitespaces in between the string, scanf() takes in only the characters before the first space. This can be avoided by the gets() function.



Input using gets()

Again using fgets() function,


Input using fgets()

5. C String Library functions

The standard C library incorporates many in-built string oriented functions. All these functions are available inside the string.h header file. So, including the string.h header file is going to be useful for easier string manipulation.

Some common functions are,

  • strlen(string) – This function returns the length of the passed string or the total number of elements in the string,
  • strcmp(string1,string2) – This function compares two string. It returns 0 if the two strings are equal, and else it returns a non-zero value,
  • strcat(string1,string2) – This function concatenates two given strings. It Appends string2 to the end of string1 and stores the result to string1,
  • strcpy(string1,string2) – This function copies the contents of the string2 into the string1,
  • strrev(string) – This function reverses the string inside,
  • And many more.

For further reading – Check all the previous tutorials on C Programming on Journaldev.

6. References:

By admin

Leave a Reply

%d bloggers like this: