What is String?

A string is a sequence of characters. String is a sequence of characters that is treated as a single data item and terminated by null character '\0'.
a C string can be defined as an array of characters.

Null-terminated string :-

In C, a string is terminated by a special character called null character ( \0). When you define a string, you must reserve a space for the ( \0) null character. For example, to declare a string that holds 5 characters, the size of the array must be at least 6.
char str[6];

For example: The string "hello world" contains 12 characters including '\0' character which is automatically added by the compiler at the end of the string.

Declaring and Initializing a string variables :-

There are two ways to declare string in c language
  • By char array
  • By string literal
  • declaring string by char array in C language.


    char ch[9]={'i', 't', 'e', 'c', 'h', 't', 'u', 't', 'o', '\0'};
    Remember that when you initialize a character array by listing all of its characters separately then you must supply the '\0' character explicitly.

    While declaring string, size is not mandatory. So you can write the above code as given below:
    char ch[]={'i', 't', 'e', 'c', 'h', 't', 'u', 't', 'o', '\0'};

    Some examples of illegal initialization of character array are,
    char ch[3] = "hell"; // Illegal
    char str[4];
    str = "hell"; // Illegal

    define string by string literal in C language. :-

    char address[]={'P', 'A', 'T', 'N', 'A', '\0'};

    char ch[]="PATNA";
    n the above declaration NULL character (\0) will automatically be inserted at the end of the string. In such case, '\0' will be appended at the end of string by the compiler.
    '\0' represents the end of the string. It is also referred as String terminator & Null Character.


    #include<stdio.h>
    #include<stdio.h>
    int main()
    {
    /* String Declaration*/
    char nickname[20];
    printf("Enter your Nick name:");
    scanf("%s", nickname);
    /*Displaying String*/
    printf("%s",nickname);
    return 0;
    }

    outout :- Enter your Nick name:itechtuto
    itechtuto


    #include<stdio.h>
    #include<stdio.h>
    int main()
    {
    /* String Declaration*/
    char nickname[20];
    /* Console display using puts */
    puts("Enter your Nick name:");
    /*Input using gets*/
    gets(nickname);
    puts(nickname);
    return 0;
    }


    Difference between char array and string literal :-

    The only difference is that string literal cannot be changed whereas string declared by char array can be changed.

    #include<stdio.h>
    #include<stdio.h>
    int main(){
    char ch[10]={'i', 't', 'e', 'c', 'h', 't', 'u', 't', 'o', '\0'};
    char ch2[10]="itechtuto";
    printf("Char Array Value is: %s\n", ch);
    printf("String Literal Value is: %s\n", ch2);
    return 0;
    }


    String Input and Output :-

    Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on the first white space it encounters. Therefore if you try to read an input string "Hello World" using scanf() function, it will only read Hello and terminate after encountering white spaces.
    However, C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including white spaces.

    #include<stdio.h>
    #include<string.h>
    void main()
    {
    char str[20];
    printf("Enter a string");
    scanf("%[^\n]", &str); //scanning the whole string, including the white spaces
    printf("%s", str);
    }

    gets() function :-

    Another method to read character string with white spaces from terminal is by using the gets() function.
    char text[20];
    gets(text);
    printf("%s", text);

    gets() and puts() functions :-

    The gets() function reads string from user and puts() function prints the string. Both functions are defined in <stdio.h> header file.

    #include<stdio.h>
    #include<string.h>
    int main(){
    char name[50];
    printf("Enter your name: ");
    gets(name); //reads string from user
    printf("Your name is: ");
    puts(name); //displays string
    return 0;
    }

    String Functions :-

    C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in string.h library. Hence, you must include string.h header file in your programs to use these functions.




    strlen() function :-

    The strlen() function returns the length of the given string. It doesn't count null character '\0'.

    #include<stdio.h>
    #include<string.h>
    int main(){
    char ch[20]={'i', 't', 'e', 'c', 'h', 't', 'u', 't', 'o', '\0'};
    printf("Length of string is: %d",strlen(ch));
    return 0;
    }
    Length of string is: 9

    strcpy() :-

    The strcpy(destination, source) function copies the source string in destination.

    #include<stdio.h>
    #include<string.h>
    int main(){
    char ch[20]={'i', 't', 'e', 'c', 'h', 't', 'u', 't', 'o', '\0'};
    char ch2[20];
    strcpy(ch2,ch);
    printf("Value of second string is: %s",ch2);
    return 0;
    }

    String Concatenation: strcat()

    #include<stdio.h>
    #include<string.h>
    int main(){
    char ch[10]={'i', 't', 'e', 'c', 'h', '\0'};
    char ch2[10]={'c', '\0'};
    strcat(ch,ch2);
    printf("Value of first string is: %s",ch);
    return 0;
    }

    Output
    Value of first string is: helloc

    Compare String: strcmp()

    The strcmp(first_string, second_string) function compares two string and returns 0 if both strings are equal.

    #include<stdio.h>
    #include<string.h>
    int main(){
    char str1[20],str2[20];
    printf("Enter 1st string: ");
    gets(str1);//reads string from console
    printf("Enter 2nd string: ");
    gets(str2);
    if(strcmp(str1,str2)==0)
    printf("Strings are equal");
    else
    printf("Strings are not equal");
    return 0;
    }

    output
    Enter 1st string: hello
    Enter 2nd string: hello
    Strings are equal

    Reverse String: strrev()

    #include<stdio.h>
    #include<string.h>
    int main(){
    char str[20];
    printf("Enter string: ");
    gets(str);//reads string from console
    printf("String is: %s",str);
    printf("\nReverse String is: %s",strrev(str));
    return 0;
    }

    output

    itechtuto
    otuthceti

    String strstr()

    The strstr() function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character.
    Syntax:
    char *strstr(const char *string, const char *match)


    #include<stdio.h>
    #include<string.h>
    int main(){
    char str[100]="this is itechtuto with c and java";
    char *sub;
    sub=strstr(str,"itech");
    printf("\nSubstring is: %s",sub);
    return 0;
    }

    output

    itechtuto with c and java





    Previous Next

    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here


    Ittutorial