String

String: A string is a set of character and also it is defined as an array of characters. Difference between character array and string is -- a string is terminated with a special character ‘\0’ but character is not .


Declaration of strings variable: Declaring a string is sam as declaring a one dimensional array. Below is the basic syntax for declaring a string.

    char string_name[n];//here n is how many character you want to store.

Above syntax string_name is any name given to the string variable and size is used define the length of the string. [Note:the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate termination of string which differs strings from normal character arrays. ]

String can be initialized in several ways.Let's see the following example to declare a string with name as str and initialize it with “tutorial”.

//1st way 
char num[] = {'t','u','t','o','r','i','a','l','\0'};
//2nd way 
char num[] = "tutorial";
//3rd way
char num[8];
num[0] = 't';
num[1] = 'u';
num[2] = 't';
num[3] = 'o';
num[4] = 'r';
num[5] = 'i';
num[6] = 'a';
num[7] = 'l';
 

We must remember that '\0' is call null character and it is also call string terminator. All the above way generate following sring

string example

Let's familiar with the String Input:

char str[100];
//1st way using scanf
scanf("%[^\n]%*c", str); 

//2nd way using gets
gets(str);
        
//3rd way using loop
for(int i=0;i< string_length;i++){
    scanf("%c", str[i]);
 }

In the first way ^\n mean that take input until user press enter button or newline. In the second way gets() is a build in funtion to input the string. In the third way using loops you can also take input as much as you want but one thing I want to mention that string lenght must shoter than the sting array size . Let's see the following example then everythinh will be clear.

#include<stdio.h>
  int main(){
     char str[30];
     for (int i=0;i< 19;i++){
         scanf("%c", str[i]);
     }
     printf("%s",str);
     return 0;
  }
Input:
   tutorialforbeginner
Output:
    tutorialforbeginner
Best way of learing

Self learing is the best learing in the world. Discover yourself first then will get what you are And what you want to do .It will push you for self learing.

Why you need to learn coding?

Coding will play a vital role in one's life . It will help to open a new window of thinking . You can think better way than past . It helps to organise all the thing in better way .