String

  • String is the char type array in which elements are various characters.
  • Each character is a an individual element.
  • every element has an Unique index number.
  • With the help of Index number ,You can access elements of strings
  • Character arrays has a '\0' which is a null character at the end as a last element of the character array.
  • Even if you have not specified the '\0' character at the end but the compiler  will automatically place the '\0' character at the end.
  • There is no need of specifying the '\0' null character at the end ,while initializing the character array ,or while giving the input as string, because  Compiler automatically assign '\0' character at the end

Declaration of String

Syntax:

datatype name[size_of_char_array];

Let's declare some character array.

#include<stdio.h>

void main()
{
    char char1[20]; //this char array can store 20 character elements.
    
    
    char char2[10];  //this char array can store 10 character elements.
}


Initialization of character array.

  • Initializing of character array can done in multiple ways.
  • Let's see them.

#include<stdio.h>

void main()
{
    char char1[5] = {'a','b','c','d','e'}; //One of the way of initialization.
    
    
    
    
     //another way of initialization, and this is a better way of initializing.
    //whole char array is initialized under the double quotes.
    
    char char2[5] = "abcde";     //each character is a an element.
                               //each element has index number.
//here index  of characters are a=0,b=1,c=2,d=3,e=4.

//another way of initializing char array is without specifying the size of array.

   char  char3[] = "abcde";

   char char4[]  = {'a','b','c','d','e'};//without specifying the size.
}

Below image will give the view of how strings are stored in memory.




Accessing the elements.

  • As you know every element in character array has an unique index number.
  • With the help of index number we will access the elements.
  • let access some  elements of character array.

#include<stdio.h>

void main()
{  char first_char1,second_char1;
   char first_char2,second_char2;
    char char1[5] = {'a','b','c','d','e'};

     //Accessing elements of char1 array.
    first_char1  = char1[0];  //assigning first element of char1 array which is 'a' to a variable first_char1.
    second_char1  = char1[1]; //assigning second element of char1 array which is 'b' to a variable second_char1.

    printf("\nFirst element of char1 array is %c and second element  is %c",first_char1,second_char1);


    char char2[10] = "Helloworld";



     //Accessing elements of char1 array.
     first_char2  = char2[0];  //assigning first element of char2 array which is 'H' to a variable first_char2.
    second_char2  = char2[1]; //assigning second element of char2 array  which is 'e' to a variable second_char2.


    printf("\nFirst element of char2 array is %c and second element is  %c",first_char2,second_char2);

}

Output:
First element of char1 array is a and second element is b.
First element of char2 array is H and second element is e.

How to take string as input?

  • taking string as input can done with scanf() function
  • You can take string as input and print on the screen by using specifier %s
  • lets take some input and print them on screen.
    Syntax:

    scanf("%s",var);  // taking string as input.
      //for taking string as input there  is no need of writing '&' operator
    printf("%s",var);  //printing the string. 
    

    lets see simple program.

    #include<stdio.h>
    
    void main()
    {
        char s[10];
    
        printf("Enter any string:");
        scanf("%s",s);
    
        printf("\nstring is %s",s);
    
    }
    

    Output:
    Enter any string: Programmer
    string is Programmer
  • But ,scanf() and printf() have a one drawback.
  • This function can't accept or print the string containing space.
  • this function take space as deliminator
  • Let's understand it by program

    #include<stdio.h>
    
    void main()
    {
        char s[10];
    
        printf("Enter any string:");
        scanf("%s",s);
    
        printf("\nstring is %s",s);
    
    }
    

    Output:
    Enter any string:Programmers are great
    string is Programmers
    This is how printf() and scanf() does not work on string containing spaces.
  • To overcome from this drawback ,'C' language provides built-in functions
  • gets() and puts() You can use this function while working with string containing spaces.
  • gets() & puts()



Further Concepts: