Strings In C Programming

Filed Under: C Programming
Strings in C Programming

Strings in C programming are an array of characters with a NULL character ('\0') 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 ('\0') too.

char demo[10];

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’ , ‘\0’ };
  3. char string[]=”JournalDev”;
  4. char string[]= { ‘J’ , ‘o’, ‘u’ , ‘r’ , ‘n’ , ‘a’ , ‘l’ , ‘D’ , ‘e’ , ‘v’ , ‘\0’ };

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 '\0').

3. Traversing a String in C

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

char string[20]="JournalDev";
char ch;
int i=0;
ch=string[i];
while(ch!=NULL)
{
     printf("%c",ch);
     i++;
     ch=string[i];
}

Output:

JournalDev

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,

#include<stdio.h>
int main()
{
    char string_input[20];
    printf("Enter the string: ");
    scanf("%s", string_input);
    puts(string_input);
}

Output:

Input Scanf
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.

#include<stdio.h>
int main()
{
    char string_input[20];
    printf("Enter the string: ");
    gets(string_input);
    puts(string_input);
}

Output:

Input Gets
Input using gets()

Again using fgets() function,

#include<stdio.h>
int main()
{
    char string_input[20];
    printf("Enter the string: ");
    fgets(string_input,sizeof(string_input),stdin);
    puts(string_input);
}

Output:

Input Fgets
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:

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages