String in C

String in C

What is String?

A string is a sequence of characters as a one-dimensional array, terminated by a null character ('\0'). In simple word a String is defined as an array of characters that ends with a null character ('\0').

How to declare a String?

Declaring of a String is similar as declaring an one-dimensional Array.

Syntax :

char string_name[size];

Example :

char eruditors[10];

//here char indicates the data type
//eruditors is the name of the string
//the size of the string is 10, defined inside the ‘[ ]’

How to initialize a String?

A string can be initialized in different ways. There are two main ways to initialize a string in c language.

1.By char array

char str[10]={'e', 'r', 'u', 'd', 'i', 't', 'o', 'r', 's', '\0'};

//we can initialize a string without specifying the string size also. For example,

char str[]={'e', 'r', 'u', 'd', 'i', 't', 'o', 'r', 's', '\0'};

//here in both cases we are initializing strings named ‘str’ which contains the value ‘eruditors’

2.By string literal

char str[10]="eruditors";

//now, we’ll declare without declaring the string size

char str[]="eruditors";

//here in both cases we are initializing strings named ‘str’ which contains the value ‘eruditors’

How to read a string from a user?

String is a derived data type. It is denoted by '%s'. When printing or taking a user input as a String we can simply do it by '%s' format specifier.

Syntax :

char string_name[ ];
scanf(" %s ",string_name);

//here at first we are creating a string named string_name
//then we are taking a string as user input using ‘scanf’ and ‘%s’ format specifier

String Input/Output Program in C :

#include <string.h>

int main()
    char str1[]={'e', 'r', 'u', 'd', 'i', 't', 'o', 'r', 's', '\0'};    
    char str2[]="eruditors";  
    char str3[25];

    printf("Input the string you wish to print : \n");
    printf("'Char Array' String is : %s\n", str1);    
    printf("'String Literal' String is : %s\n", str2); 
    printf("'User Given' String is : %s\n", str3);  
    return 0;

Output :

String operations using C Library Functions :

A programmer needs to do many kinds of works with Strings like copy, paste, merge and many more. It becomes very complicated if we write lines of codes to perform these tasks. C standard libraries contains many functions that do those works very easily. "string.h" header file contains many useful functions that makes coding very easy. For example,

gets() - takes input as a string from user
puts() - prints a copied string
strcpy() - copies a string to another
strcat() - concatenates(joins) two strings
strlen() - computes string's length
strcmp() - compares two strings
strchr() - returns a pointer of the first character of a string
strlwr() - converts string to lowercase
strupr() - converts string to uppercase

Key Points about String :

  • ~ The difference between a character array and a string is the string is terminated with a special character '\0'
  • ~ Each character in the array occupies one byte of memory.
  • ~ The literal string cannot be reassigned to another value whereas, characters of the array can.
  • ~ To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in that word.