Strings in C

In this section of the tutorial, we will discuss a special case of characters used in a program i.e. Strings.

We will learn how to declare and initialize Strings in a program, how to read Strings from user input, how to read a line of text, how to print Strings to the output screen, and other features of Strings with program examples for better understanding.

Now let’s move on to the introduction of Strings in C.

What are Strings?

A String is a sequence of characters that is treated as a single unit or data item in the program. A group of characters defined between double quotation marks is called a String Constant.

Example:

“Welcome to Codedec.”

[Important note: For printing a double quote, we can use it along with a backslash.]

Example:

“\” Welcome to Codedec,\” said the instructor.”

We can use function prinntf() to print a statement:

printf(“\”Welcome to Codedec.”\”);

The output will be:

“Welcome to Codedec.”

And if we remove the inner double quotation and backslash, it will print the following statement:

Welcome to Codedec.

Character Strings are used to write meaningful and readable programs.

Some of the operations we can perform using Character Strings are:

  • Reading and writing Strings.
  • Combining Strings together.
  • Copying one String to another String.
  • Comparing between two Strings.
  • Extracting a portion of a String.

Further, we will discuss each of these operations in detail along with the library functions that are used to implement these operations.

How to declare String variables?

In C programming language, we can represent Strings as Character Arrays. So we can name a String by any valid variable names and it is always declared as an Array of Characters.

General form of declaring a String variable can be: char string_name[size];

  • Here, char indicates the data type of the String i.e character type.
  • string_name indicates the name of the String.
  • size indicates the number of characters in the String.

Example:

char name[20];
char country[10];

[Important note: A NULL character (\0) is assigned by default at the end of the String by the compiler.]

How to initialize String variables?

Character Arrays can be initialized when they are declared. We can declare a Character Array by either of two ways given below:

char country[10] = “SRI LANKA”;                          
char country[10] = {‘S’,’R’,’I’,’ ‘,’L’,’A’,’N’,’K’,’A’,’\0′};

  • Here, country has 10 elements with a space element and a NULL character (\0).
  • When we initialize a Character Array, we must input the NULL terminator.

We can also initialize a Character Array without specifying the number of elements. In this case, the size of the Character Array will be equal to the number of elements initialized.
Example:

char line [ ] = {‘C’,’O’,’D’,’E’,’D’,’E’,’C’,’\0′};

  • Here, the Character Array line has a size of 8 elements.

We can also declare a Character Array’s size larger than the size of the String initialized.
Example:

char name[12] = “CODEDEC”;

  • Here, Character Array name has a size of 12 elements but String size is 7.
  • So, the leftover elements will be initialized to NULL.

The Character Arrays distribution will look like this:

But we cannot declare a Character Array’s size smaller than the size of String initialized.
Example:

char name[4] = “CODEDEC”;

This will produce error in program.

How to read Strings from user?

Strings can be read from many different functions present in the C library, each of which is discussed below in detail.

Using Function scanf()

Function scanf() can be used to read Strings from user input. We use the format specifier %s to read the character values.
Example:

char country[10];        
scanf(“%s”, country);

[Important note: An ampersand ‘&‘ is not required before the variable name for Character Arrays input.]

But function scanf() has its own limitations as it will read the input only till the characters are given, it will self terminate if any white space is encountered.
Example:

SRI LANKA

Here, only the String SRI will be read into the Array address because the blank space after the word ‘SRI‘ will be encountered and the function will be terminated before reading the next String ‘LANKA‘.

Hence, the distribution of the address Array will be in a way like this:

Now, if we want to read the entire line SRI LANKA, then we have to use two separate Character Arrays of required sizes.
Example:

char name1[6], name2[6];         
scanf(“%s %s”,name1,name2);

  • Here, String ‘SRI‘ will be assigned to name1 and String ‘LANKA‘ will be assigned to name2.

The distribution of the address Arrays will be in a way like this:

Another useful thing is that we can specify the field width using a specifier form %ws for reading only the specified number of characters from the input strings.
Example:

scanf(“%ws”, country);

This function instructs the compiler to read only the number of character that is equal to the specified size.
Example:

char country[12];          
scanf(“%6s”, country);

Then the input String for the words ‘INDIA‘ and ‘AMERICA‘ will be as such:

Below is an example to demonstrate the use of function scanf().

Example: Program to print the name of the user.

Code:

#include<stdio.h>
int main()
{
    char s1[10];
    printf("Enter your name: ");
    scanf("%s",s1);
    printf("Name: %s\n",s1);
    return 0;
}

Output:

Enter your name: Shahbaz
Name: Shahbaz

Using Function getchar() and gets()

Function getchar() is used to read a single character. We can use this function multiple times to input characters and then later put it together in an Array. Likewise, an entire line can be stored in an Array.
Then reading is terminated when newline character ‘\n’ is encountered and then a NULL character is added at the end.
Example:

char name;
name = getchar();

[Important note: Function getchar() has no parameter.]

Another useful method to read a line of text including white spaces is by using Function gets(). This Function has only one string parameter.
A general form of Function call can be: gets(name);
Example:

char name[10];         
gets(name);               
printf(“%s”, name); 

  • Here, name is variable declared, which reads characters into name until a new line character ‘\n’ is encountered.
  • At the end of the string, it puts a NULL character.
  • Unlike Function scanf(), this function does not skip white spaces.

Reading Multiple Characters

Function scanf() with format specifier %s or %ws can only read Strings without white spaces. It is not optimal to be used to print a line containing multiple words with white spaces in between.

But we can use an edit set conversion code %[..] to read a line containing a variety of characters including white spaces.
Example:

char name[20];               
scanf(“%[^\n]”, name);
printf(“%s”, name);     

  • Here, a line of input having characters as well as white spaces from the user will be read and displayed on the screen.

Below is an example to demonstrate how we can read multiple characters.

Example: Program to print a line of text.

Code:

#include<stdio.h>
int main ()
{
    char string1[5],m;
    int i=0;
    printf("Enter a line of text: ");

    do
    {
        m = getchar();
        string1[i] = m;
        i++;
    }
    while (m != '\n');
    i=i-1;
    string1[i] = '\0';
    printf("You entered: %s\n",string1);
    return 0;
}

Output:

Enter a line of text: Welcome to Codedec.
You entered: Welcome to Codedec.

How to write Strings to screen?

Strings can be written to screen by using many different functions present in the C library, each of which is discussed below in detail.

Using Function printf()

As we have seen in previous sections of the tutorial, Function printf() can be used %s format specifier to print a text. The format %s can be used to display Arrays of Characters.
Example:

printf(“%s”, name);

  • Here, entire contents of the array name will be displayed.

We can also specify precision with the %s format, like:

%10.4

  • Here, it indicates that the first four characters have to be printed in a field width of 10 columns.

Below is an example to demonstrate how we can write Strings using %s format specifier.

Example: Program to write Strings using %s format specifier.

Code:

#include<stdio.h>
int main()
{
    char string1[10]="CODEDEC";
    printf("%8s\n",string1);
    printf("%5s\n",string1);
    printf("%10.4s\n",string1);
    printf("%-10.4s\n",string1);
    printf("%10.6s\n",string1);
    printf("%.3s\n",string1);
    printf("%s\n",string1);
    return 0;
}

Output:

 CODEDEC
CODEDEC
      CODE
CODE      
    CODEDE
COD
CODEDEC

Using Function putchar() and put()

Function putchar() prints the value of character variables, in the form given below:

char ch = ‘M’;
putchar (ch);  

  • Here, Function putchar() requires one parameter. We can use this function multiple times to print a String of characters using a for loop.

Example:

char name[8] = “INDIA”
for (i=0; i<5; i++)             
putchar(name[i];             
putchar(‘\n’);                   

Another way of printing string values is by using Function puts(). This function also requires only one parameter.
General form of this function is:

put (str);

  • Here, str is a String variable containing String values.

Example:

char country[20];
get (country);      
puts (country);    

  • Here, a line of text is read and displayed on the screen.

Arithmetic Operations On Characters

In C programming language, we can perform arithmetic operations in the same way that we do with numbers. Because whenever a character constant is used in an expression, it is automatically converted into an integer value by the machine.

Now, to represent a Character in its Integer form, we may write it as an Integer.
Example:

ch = ‘a’;                
printf(“%d”, ch);

  • Here, number 97 will be displayed on the screen. Hence, it is possible to perform arithmetic operations on character constants and variables.

Example:

x = ‘k’ – 1;

  • Here, the above statement is valid. The value of ‘k‘ is 75 in the machine, therefore, the statement will modify the value in variable x to 74.

Relational operations can also be performed with Character constants.
Example:

ch >= ‘X’ && ch <= ‘Y’

There’s another function in C, that converts a String of digits into their integer values.

General form of this function is:

x = atoi(string);

Example:

number = “2020”;    
year = atoi(number);

  • Here, number is a String variable assigned to the String constant “2020”.

The function atoi() converts the String “2020” to the numeric equivalent value of 2020 and assigns it to the integer variable year.

Below is an example to print the set of alphabets in decimal and character forms.

Example: Program to print alphabets in their decimal form.

Code:

#include<stdio.h>
int main()
{
    char i;
    for ( i = 65; i <= 122; i=i+1)
    {
        if(i>90 && i<97)
            continue;
        printf(",%4d - %c ",i,i);
    }
    printf(",\n");
    return 0;
}

Output:

,  65 - A ,  66 - B ,  67 - C ,  68 - D ,  69 - E ,  70 - F ,  71 - G ,  72 - H ,  73 - I ,  74 - J ,  75 - K ,  76 - L ,  77 - M ,  78 - N ,  79 - O ,  80 - P ,  81 - Q ,  82 - R ,  83 - S ,  84 - T ,  85 - U ,  86 - V ,  87 - W ,  88 - X ,  89 - Y ,  90 - Z ,  97 - a ,  98 - b ,  99 - c , 100 - d , 101 - e , 102 - f , 103 - g , 104 - h , 105 - i , 106 - j , 107 - k , 108 - l , 109 - m , 110 - n , 111 - o , 112 - p , 113 - q , 114 - r , 115 - s , 116 - t , 117 - u , 118 - v , 119 - w , 120 - x , 121 - y , 122 - z ,

Combining Two Strings

The process of combining two Strings together is called Concatenation. Strings cannot be combined together using arithmetic operations.

In order to combine two Strings, we need a third String large enough in size to hold the total characters of both the Strings in one place.

Below is an example to demonstrate how we can concatenate multiple Strings.

Example: Program to combine three Strings together.

Code:

#include<stdio.h>
int main()
{
  int i,j;
  char s1[10] = {"WELCOME "};
  char s2[10] = {"TO "};
  char s3[10] = {"CODEDEC"};
  char result=0;
  
  while (s1[result] != '\0')
  {
    ++result;
  }

  for ( i = 0; s2[i] != '\0'; ++i, ++result)
  {
    s1[result] = s2[i];
  }

  for ( j = 0; s3[j] != '\0'; ++j, ++result)
  {
    s1[result] = s3[j];
  }

  s1[result] = '\0';

  puts(s1);

  return 0;
}

Output:

WELCOME TO CODEDEC

Comparing Two Strings

Strings can be compared with each other only after testing it character by character. It cannot be compared directly as a whole.

The comparison is done until any mismatch is encountered or one of the Strings terminates into a NULL character.

We can understand how we can compare two Strings in the program given below:

Example: Program to compare two Strings.

Code:

#include<stdio.h>
int main()
{
    int i=0;
    char s1[8] = "CODEDEC";
    char s2[8] = "CODEDEC";
    while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
    {
        i = i+1;
    }
    if (s1[i] == '\0' && s2[i] == '\0')
    {
        printf("Equal\n");
    }
    else
    {
        printf("Not Equal\n");
    }
    return 0;
}

Output:

Equal

String Functions

Strings can be manipulated in different ways using various String-handling Functions present in the C library.

Some of the common and most used String-handling Functions are:

  1. strcat() – It concatenates two Strings.
  2. strcmp() – It compares two Strings.
  3. strcpy() – It copies one String over another.
  4. strlen() – It finds the length of a String.

There are many more useful String-handling function used in C:

  • strncpy()
  • strncmp()
  • strncat()
  • strstr()

Now, we will discuss the use of each of these String-handling functions one by one.

Function strcat()

This function joins two Strings together.
General form:

strcat(string1,string2);

  • Here, string1 and string2 are two Character Arrays.
  • When function strcat() is executed, string2 is appended to string1 by removing the NULL character at the end of string1 and placing string2 from there.
  • string2 remains unchanged.

Example:
Suppose the distribution of Strings is as follows:

It will become:

[Important note: The size of string1 in which string2 is to be appended must be large enough to accommodate string2.]

Function strcmp()

This function compares two Strings identified by the arguments in it and has a value 0 if both the Strings are equal.

The general form of this function is:

strcmp(string1,string2);

  • Here, string1 and string2 can be String variables or String constants.

Example:

strcmp(“here”, “hear”);

  • Here, The function will not return 0 value, stating that both the Strings are not equal.

Function strcpy()

This function assigns the content of one String to another.

General form of the function is:

strcpy(string1, string2);

  • Here, the contents of string2 are assigned to string1.
  • string2 may be a Character Array or a String Constant.

Example:

strcpy(result, “PASS”);

  • Here, the String ‘PASS‘ will be assigned to the String variable result.

[Important note: The size of the String variable in which the contents are to be assigned should be large enough to accommodate the contents.]

Below is an example to demonstrate the use of function strcpy().

Example: Program to copy Strings using function strcpy().

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char s1[30]= "WELCOME TO CODEDEC!";
    char s2[30];
    char s3[30];
    strcpy(s2, s1);
    strcpy(s3, "Happy Learning :)");
    puts(s2);
    puts(s3);
    return 0;
}

Output:

WELCOME TO CODEDEC!
Happy Learning :)

Function strlen()

This function counts the number of characters in the String and returns the value to the function.

General form of this function is:

n = strlen(string);

  • Here, n is an integer variable that receives the count value from the function.
  • string can be a String constant or String variable.
  • The counting ends when a NULL character is encountered.

Function strncpy()

This function copies only the left-most n characters of one String to another String. It is a three-parameter function.

The general form of this function is:

strncpy(string1, string2, 4);

  • Here, only 4 characters of the source string2 will be copied to the target string1.

We must declare explicitly assign the NULL character in the string2 to avoid any error.
Example:

string1[5] = ‘\0’;

Function strncmp()

This function compares the left-most n characters of string1 and string2. It is a three-parameter function.

General form of this function is:

strncmp(string1, string2, n);

  • Here, string1 will be compared with string2 and then three kinds of values may be returned by the function.
  • Value 0, if both the Strings are equal.
  • A negative value, if string1 is less than string2.
  • A positive value, if string1 is greater than string2.

Function strncat()

This function concatenates two Strings. It is a three-parameter function.

The general form of this function is:

strncat(string1, string2, n);

  • Here, the function will concatenate the left-most n characters of string2 to the end of string1.
    Example:

Afterr function call strncat(string1, string2, 3);
The result will be:

Function strstr()

This function is used to locate a sub-string in a string. It is a two-parameter function.

The general form of this function is:

strstr(string1, string2);
or
strstr(string1, “ABC”);

  • Here, function strstr() searches the string1 whether string2 is present in string1 or not.
  • If it is present, the function returns the position of the sub-string.
  • If it is not present, the function returns a NULL pointer.

List of Character Strings

The need of using a list of Character Strings often arises when we want to store a list of character data in a program. This list of Character Strings is stored in a two-dimensional character array.

Example:

char country[ ] [ ]
{
  “India”;
  “Nepal”;
  “America”;
  “Bangladesh”;
  “Turkey”;
};

The distribution of the list of Strings can be in a way like this:

Now, we can access the name of the ith country in the list by using:

country[i-1]

  • Here, we are treating a two-dimensional array as a one-dimensional array by accessing the table of Strings as a column of Strings.

Character Strings and Pointers

We can declare and initialize a Character Array like this:

char str[6] = “HELLO”;

The compiler will automatically put a NULL character at the of the String ‘HELLO‘.

In C programming language, we can also create Strings using Pointer variables.
Example:

char *str = “HELLO”;

  • Here, a String will be created and its address will be stored in the Pointer variable str.
  • The Pointer variable points to the first character of the String ‘HELLO’.

The String characters can be stored in a Character Array in a way like this:

We can store values in a String Pointer at run-time operation too.
Example:

char * str;      
str = “Hello”;

  • Here, str is Pointer that is assigning the String values.
  • Don’t confuse it with String copying which is not allowed in C.

Now, we can print the stored value of variable str using either Function printf() or Function puts().
Example:

printf(“%s”, str);
puts (str);              

  • Here, str is a String variable and also a Pointer variable. So we need not use the indirection operator ‘*‘.

Passing Strings to Functions

We can pass Strings to Function in the same way we pass Arrays to Function. We must follow a few rules before carrying out the operation, that are given below:

1. The general form of passing Strings to Functions is:

void country(char item_name[ ])
{
..
..
}

2. The Function declaration must String as an argument.
Example:

void country(char str[ ]);

3. The Function call must have the String Array name without index value as its actual argument.
Example:

display (str);

  • Here, str is String Array declared in the calling Function.

[Important note: Strings cannot be passed by value to the Functions.]

Few more examples on Strings in C

Example: Program to count the number of words and characters in a line.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char line[50], ctr;
    int i,c;
    int end=0,characters=0,words=0;
    printf("Enter a text: ");
    while (end==0)
    {
        c=0;
        while ((ctr=getchar()) != '\n')
        {
            line[c++] = ctr;
            line[c] = '\0';
        }
        if (line[0] == '\0')
        {
            break;
        }
        else
        {
            words++;
            for ( i = 0; line[i] != '\0'; i++)
            {
                if (line[i] == ' ' || line[i] == '\t')
                {
                    words++;
                }
            }
        }
        characters = characters + strlen(line);
        break;
    }
    printf("\n");
    printf("Number of words: %d\n",words);
    printf("Number of characters: %d\n",characters);
    return 0;
}

Output:

Enter a text: Welcome to Codedec!

Number of words: 3
Number of characters: 19
Example: Program to demonstrate the use of String-Handling Functions.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char s1[10],s2[10],s3[10];
    int n,i,j,k;

    printf("Enter two Strings: ");
    scanf("%s %s",s1,s2);

    n = strcmp(s1,s2);
    if(n != 0)
    {
        printf("\nNot Equal\n");
        strcat(s1,s2);
    }
    else
    {
        printf("\nEqual\n");
    }

    strcpy(s3,s1);
    i = strlen(s1);
    j = strlen(s2);
    k = strlen(s3);
    printf("Length of String %s is %d characters\n",s1,i);
    printf("Length of String %s is %d characters\n",s2,j);
    printf("Length of String %s is %d characters\n",s3,k);
    
}

Output:

Enter two Strings: SRI LANNKA

Not Equal
Length of String SRILANNKA is 9 characters
Length of String LANNKA is 6 characters
Length of String SRILANNKA is 9 characters
Example: Program to reverse a String.

Code:

#include<stdio.h>
#include<string.h>
 
void Rev(char*);
 
int main()
{
    char s1[100];
 
    printf("Enter a String: ");
    gets(s1);
    Rev(s1);
    printf("Reverse order of the entered String is \"%s\".\n",s1);
    return 0;
}
 
void Rev(char *s1)
{
    int i,j;
    char *start, *end, temp;
    j = strlen(s1);
    start = s1;
    end = s1;
 
    for (i = 0; i < j - 1; i++)
        end++;
 
    for (i = 0; i < j/2; i++) { 
        temp = *end;
        *end = *start;
        *start = temp;
 
        start++;
        end--;
    }
}

Output:

Enter a String: CODEDEC
Reverse order of the entered String is "CEDEDOC".

In the next section of the tutorial, we will discuss two constructed data types known as Structures and Unions in detail.