C Programming String, Array of Characters

Advertisements

String in C

A string is a collection of characters, stored in an array followed by null ('\0') character. Null character represents the end of string.


Address of first element is random, address of next element depend upon the type of array. Here, the type is character and character takes one byte in memory, therefore every next address will increment by one.

Index of array will always starts with zero.

Declaration of String or Character array

Declaration of array means creating sequential bolcks of memory to hold fixed number of values.

Syntax for string declaration :


		char String-name[size of String ];

Example for string declaration :


		char String [25];				//Statement 1

In the above example we have declared a character array which can hold twenty-five characters at a time.

Initialization of String

Initialization of string means assigning value to declared character array or string.

Examples 1 :Using sequence of characters.


       char String [ ] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};

In the above example, we are declaring and initializing a string at same time. When we declare and initialize a string at same time, giving the size of array is optional and it is programmer's job to specify the null character at the end to terminate the string.

Example 2 : Using assignment operator


       char String [ ] = "Hello World";

In this approch, programmer doesn't need to provide null character, compiler will automatically add null character at the end of string.

Input string using getche() function

The getche() read characters from console (output window) one by one and put these characters into string using loop.

Examples for input string with getche() function


       #include<stdio.h>

       void main()
       {
              char String [50];
              char ch;
              int i;

              printf("\n\n\tEnter your name : ");
              for(i=0;i<50;i++)
              {
                     ch = getche();         //Statement   1

                     if(ch==13)               //Statement   2
                     break;

                     String [i] = ch;
              }

              String [i] = '\0';                      //Statement   3

              printf("\n\n\tHello, ");
              for(i=0; String [i]!='\0';i++)
              printf("%c", String [i]);

       }

   Output :

              Enter your name : Kumar
              Hello Kumar

In the above example, A for loop will execute upto 50 time and getche() which inside for loop will read single character from console and put the character into ch. If user press the enter key, condition given in statement 2 will satisfy and terminate the loop otherwise every character will assign to String. Statement 4 is assigning null character to String.

Input string using scanf() function

The scanf() can read the entire word at a time. The format specifier for a string is "%s". The scanf() ignores the any occurrence of leading whitespace characters and terminate the string at the first occurrence of whitespace after any input.

Examples for input string with scanf() function


       #include<stdio.h>

       void main()
       {
              char String [50];

              printf("\n\n\tEnter your name : ");
              scanf("%s", String );

              printf("\n\n\tHello %s", String );

       }

   Output :

              Enter your name :      Kumar Wadhwa
              Hello Kumar

In the output of above example there is five leading whitespaces in the input which is ignored by the compiler.

Input string using gets() function

The gets() can read the entire line at a time and the string will not terminate until user press the enter key. The gets() will put all the leading and trailing whitespaces into str.

Examples for input string with gets() function


       #include<stdio.h>

       void main()
       {
              char String [50];

              printf("\n\n\tEnter your name : ");
              gets( String );

              printf("\n\n\tHello %s", String );

       }

   Output :

              Enter your name :      Kumar Wadhwa
              Hello      Kumar Wadhwa

In the output of above example there is five leading whitespaces in the input which is not ignored by the compiler.

Related topics

Advertisement