Understand String in C Programming in best way

String in C

In C Program if we want to store name, place, etc. in variable then we have to use single dimension array of character. String in C are store in form of array of character and it is terminated with special character “\0“.
\0” Symbolizes the end of the string its known as null character. It is always added after string value to indicate termination of string. This null character is the difference between normal array and string.

Declaration and Initialization of String in C

Like other datatype variable we can declare and initialized value for string(array of character) variable.

Declaration of String in C

    Syntax :

    char variable_name[size_of_array];

    Example :

    char name[20];

In above example we have declare a variable to store name and its size is 20, which means it can store 20 characters in it plus one “\0” null character to represent end of the string.

Initialization of String in C

    Syntax : 

    char variable_name[] = value

                           OR

    char variable_name[size_of_array] = value

    Example :

    char name[] = “ITVoyagers”;

    char name[20] = “ITVoyagers”;

    char name[] = { ‘I’ , ‘T’ , ‘V’ , ‘o’ , ‘y’ , ‘a’ , ‘g’ , ‘e’ , ‘r’ , ‘s’};

    char name[20] = { ‘I’ , ‘T’ , ‘V’ , ‘o’ , ‘y’ , ‘a’ , ‘g’ , ‘e’ , ‘r’ , ‘s’};

We can initialize string in C using above methods. If we are initializing value to string then there is no need to specify size of array, it will automatically take the size of the string as a size of array plus one for “\0null character.

Each character in string(array of character) is store in consecutive memory blocks.
string-in-c

Print, Accept and Manipulate string in C

We have some built-in methods which to print string and accept string from user.

In C programming we also have built-in functions to manipulate string in C.

Print string in C

Following are the functions which can be use to print the value of string variable on screen.

   printf()

   puts()

Print string in C using printf()

We can print string using printf() function.

We can write string directly in printf().

When we want to display string value from variable then we have to use “%s” format specifier, which will help us to hold place for string value in output.

To print string value from variable using printf() we have to use format specifier “%s” which makes it slow.

When we use printf() it will not move cursor to next line by default rather we have to add new line character “\n” in it.

    Example :

    printf(“ITVoyagers”)    // This will print ITVoyagers on screen

                          OR

    char name[] = “ITVoyagers”;

    printf(“Visit us on %s”,name);

Print string in C using puts()

We can also use puts() function to print string or string value from the variable on screen.

In puts() there is no need to use format specifier “%s”.

When we use puts() it will shift cursor on next line after execution.

If we don’t want cursor to go on next line then we have to pass “stdout” as a second argument in puts().

    Example :

    puts(“ITVoyagers”);                    // To print string

_____________________________

    char name[] = “ITVoyagers”;     // To print string value from variable

    puts(name);                                // After execution put() will shift cursor to next line

_____________________________

   

    char name[] = “ITVoyagers”;

    puts(name,stdout);                     // After execution put() will not shift cursor to next line

Receive string value from user in variable

Following are the functions which can be use to receive value from user in variable.

   scanf()

   gets()

Read string value using scanf()

We can read any type value in variable using scanf(), hence we can use scanf() to accept string value.

We have to use “%s” format specifier in scanf() to accept string.

scanf() can read multiple values at same time.

scanf() will stop reading when it encounters whitespace, new line charecter and end of file(EOF).

    Example : 

    #include<stdio.h>

    void main()

    {

        char name[20];

        printf(“Enter your name : “);

        scanf(“%s”,&name);

        printf(“Entered value is : %s”, name);

    }

Output :

Enter your name : ITVoyagers Official

Entered value is : ITVoyagers

We can see that scanf() will stop reading when it encounters whitespace, newline and EOF charecter. If we want scanf() to read complete string and accept whitespace as a character then we have to use “[^\n]” with format specifier.

    Example :

    #include<stdio.h>

    void main()

    {

        char name[20];

        printf(“Enter your name : “);

        scanf(“%[^n]s”,&name);

        printf(“Entered value is : %s”, name);

    }

Output : 

Enter your name : ITVoyagers Official

Entered value is : ITVoyagers Official

Read string value using gets()

We can also use gets() function to accept string value from user, gets() function can only accept string value.

In gets() there is no need to use format specifier “%s”.

gets() function stops reading when it encounters newline character and EOF.

It will continue reading string value even if it encounters whitespace, because gets() treats whitespace as a character.

    Example :

    #include<stdio.h>

    void main()

    {

        char name[20];

        printf(“Enter your name : “);

        gets(name);

        printf(“Entered value is : %s”, name);

    }

Output :

Enter your name : ITVoyagers Official

Entered value is : ITVoyagers Official

We can see that gets() will continue reading even if it encounters whitespace.

Manipulate String in C

In C programming we there are some built-in method which allows us to manipulate string.

To use builtin string functions we have to include <string.h>

strlen()        –  This function is use to find the length of string

strcat()        –  Function is use to join/concatenate 2 strings

strcpy()       –  Function is use to copy string value in variable

strcmp()     –  Function is use to compare 2 strings

strchr()       –  Function scans entire string for specified character and returns its first occurrence

strncpy()    –  Function is use to copy a part of string

strncat()     –  Function is use to concatenate part of string with other

strncmp()   –  Function is use to compare parts of strings

strrchr()      –  Function is same as strchar() but it returns last occurance of character 

strlen()

Function returns the length of a string.

It will return integer data type.

It will not count EOF character.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        printf(“Length of string is : %d”,strlen(name));

    }

Output :

    Length of string is : 10

ITVoyagers

strcat()

This function takes 2 string variables as a argument and perform concatenation on both and save result back to first argument variable.

It will not add any whitespace between strings, we have to add it in our string.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        char web[] = ” itvoyagers.in”;

        strcat(name,web);

        printf(“String is : %s”,name);

    }

Output :

    String is : ITVoyagers itvoyagers.in

ITVoyagers

strcpy()

This function takes 2 string variables as a argument and value from second argument to first argument variable. This can be use to store string value from one variable to another.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        char web[15], twitter[15];

        strcpy(web,name);

        strcpy(twitter,”@ITVoyagers”);

        printf(“String in web is : %s”,web);

        printf(“Do follow us on twitter %s”,twitter);

    }

Output :

    String in web is : ITVoyagers

    Do follow us on twitter @ITVoyagers

ITVoyagers

strcmp()

This function takes 2 strings as a argument and compare them, and returns 0 if both the strings are identical.

It will return negative number if — ASCII value of first unmatched character is less than second.

It will return positive number if — ASCII value of first unmatched character is greater than second.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        char web[] = “ITVoyagers”;

        if(strcmp(name,web)==0)

        {

            printf(“Both strings are identical”);

        }

    }

Output :

  Both strings are identical

ITVoyagers

strchr()

This function is use to find a character in a string.

Function will scan complete string for the first occurrence of searched character in string.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        char *ptr;

        ptr = strchr(name,’V’);

        printf(“String after ‘V’ is %s”,ptr);

    }

Output :

  String after ‘V’ is Voyagers 

ITVoyagers

strncpy()

This function takes 3 arguments.

First argument – variable in which we want to copy string value.

Second argument – String value which is to be copy.

Third argument – Number of characters which we want to copy.

Function is use to copy part of string to another.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        char web[15];

        strncpy(web,name,3);

        printf(“String in web is : %s”,web);

    }

Output :

    String in web is : ITV

ITVoyagers

strncat()

This function takes 3 arguments.

First argument – variable in which we want to join another string value.

Second argument – String value which is to be concatenate.

Third argument – Index position till which we want to concatenate(If index position is 12 than it will concatenate character up to 11th index position)

Function is use to concatenate a part of string to another.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        char web[] = ” itvoyagers.in”;

        strncat(name,web,12);

        printf(“String is : %s”,name);

    }

Output :

    String is : ITVoyagers itvoyagers

ITVoyagers

strncmp()

This function takes 3 arguments.

 First argument – variable in which we want to compare.

Second argument – String value which is to compare.

Third argument – Number of character to be compare.

returns 0 if both the strings are identical.

It will return negative number if — ASCII value of first unmatched character is less than second.

It will return positive number if — ASCII value of first unmatched character is greater than second.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers”;

        char web[] = “ITVoyagers”;

        if(strncmp(name,web,3)==0)

        {

            printf(“Both string parts are identical”);

        }

    }

Output :

  Both string parts are identical

ITVoyagers

strchr()

This function is use to find a character in a string.

Function will scan complete string for the last occurrence of searched character in string.

Example :

    #include<stdio.h>

    #include<string.h>

    void main()

    {

        char name[] = “ITVoyagers – Do follow us on Instagram”;

        char *ptr;

        ptr = strrchr(name,’o’);

        printf(“String after ‘o’ is %s”,ptr);

    }

Output :

  String after ‘o’ is on Instagram 

ITVoyagers

CHECKOUT OTHER RELATED TOPICS

PRACTICALS IN C PROGRAM

We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment