String in C

by | Jan 7, 2022 | C

Home » C » String in C

Introduction

As we all know, ‘words’ of the English language are made up of ‘alphabets’ and ‘sentences’ are made up of ‘words’. For example, “Programming in C language” is a sentence made from four words i.e., ‘Programming’, ‘in’, ‘C’, ‘language’. But when we are dealing with a computer language then our device never understands words, sentences, etc.

In real life, we have to deal with words, sentences, etc. but to apply such real-life examples in coding we have to make the computer understand our language. Thus, programming languages like C deals with these types of things. We all are familiar with ‘Characters’.

Definition

The String is a sequence of characters or in simple words, it is an array of characters.

‘Programming’ is not a word in the C language, it is a string.

Element‘P’‘r’‘o’‘g’‘r’‘a’‘m’‘m’‘i’‘n’‘g’‘\0’
Index 0 1 2 3 4 5 6 7 8 910 11

 

So ‘programming’ is an array of characters ‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘m’, ‘i’, ‘n’, ‘g’, and ‘\0’.

Same as in arrays, strings are also 0 based index values i.e., index value starts from 0.

All the characters of a string are written in commas.

The string is a sequence of characters terminated at null characters. A string without a null character is invalid. That’s why in the above representation ‘programming’ is terminated with ‘\0’ i.e., a null character having index value 11.

It is important to note that ‘\0’ and ‘0’ are two different characters. The ASCII value of ‘\0’ is 0 while that of ‘0’ is 48.

Declaration of the string

We can declare the string in multiple ways:

Method 1

char name[ ]= “Saumya”;

char’ is the datatype used. ‘name’ is the name of the string. char name[ ] clearly shows it is an array of characters. “Saumya” is the value of the string kept in “ ” commas.

Program

#include<stdio.h>
int main()
{
            char name[ ]="Saumya";
            printf("%s\n",name);
            return 0;
}

 

Output

Declaration of the string Method 1

Explanation

‘name’ is a ‘char’ type string that is assigned with a value “Saumya”.

Note: %s is used for the sting.

We may enter the size in the ‘name[ ]’ but it must be 7 or greater than 7 because the count of ‘S’, ‘a’, ‘u’, ‘m’, ‘y’, ‘a’, ‘\0’ is 7.  If we enter a value less than the count of all the characters then it will give an error. Therefore,

char name[ 7]=”Saumya”; is valid

char name[ 8]=”Saumya”; is valid

char name[ 6]=”Saumya”; is invalid

 

 

Method 2

char name[ ]= {‘S’, ‘a’, ‘u’, ‘m’, ‘y’, ‘a’, ‘\0’};

In the 2nd method, we have assigned the string in the form of individual characters, each one written in ‘’ commas, separated by ‘,’ and the all are enclosed in { } braces. The rest of the syntax is the same as above.

Program

#include<stdio.h>
int main()
{
            char name[ 7 ]= {‘S’, ‘a’, ‘u’, ‘m’, ‘y’, ‘a’, ‘\0’};
          printf("%s\n",name);
            return 0;
}

 

Output

Declaration of the string Method 2

Explanation

The rule for writing the size is the same for this method too as discussed in method 1. Since the count of all the characters is 7 thus giving size as 7 works fine.

Till now, we have just seen how to print the whole string at once. But we can also print individual characters.

Method 3

This method includes the declaration of the string and then the user will input the value of the string variable.

In such cases, we will declare a string variable with a size value. And ask the user to give the input. Since we don’t know the length of the string that the user will enter, so to be on the safe side, always declare the string variable with maximum size according to your assumption.

Program

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

 

Output

Declaration of the string Method 3

We declared a string variable ‘name’ with size 15. The rest of the code is quite simple.

It is important to note that we don’t use the ‘&’ symbol before the name of the string in the ‘scanf( )’ statement while taking a string input from the user because in ‘name[15]’, ‘name’ is itself the pointer to ‘name[15]’. That’s there is no ‘&’ in scanf(“%s”, name); statement.

Let’s see the same code written above once again.

Program

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

 

Output

The code is the same as the previous one but there are some changes in the output.

Declaration of the string Method 4

Explanation

Here we enter the name “Johnson Byro” but only “Johnson” gets printed without “Byro”. This is because the ‘scanf’ function takes only one word from the user to a string variable. It terminates with any white space i.e., anything written after a white space will not be considered as an input by the ‘scanf’ function and thus it will store the first string entered by the user in the string variable. That’s what happens in the above example. But this only happens when we are taking input from the user and using the ‘scanf’ function. Otherwise, whatever we enclose in “ ” braces while assigning value to the string value at the time of declaration will be considered as one string value as a whole.

Program

#include<stdio.h>
int main()
{
            char name[]="Saumya kinger D/o RK kinger";
            printf("%s",name);
    return 0;
}

 

Output

Declaration of the string Method 5

2D Array of characters

The 2D array of characters also exist and the basic configuration is the same as the 2D array of integers discussed in previous chapters.

An example of a 2D array of characters is:

char colors[3][8]={

“Red”,

“Green”,

“Yellow”

};

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author