Strings in c


 In C programming collection of characters is called String and a String is terminated by a null character. 

Example : char c[]="hello";

String variables : 

There is no separate variable to declare string in c programming, so the strings are considered to be an array of type char as shown above in the example. 

Hence a variable which used to store an array of characters is called as a String. 

Declaration of Strings

In C programming Strings are declared in a similar way like arrays , the only difference is that it is of type char. 

Syntax : char variable_name[array_length].

Example : char c[5];

The above example can hold a maximum of 5 characters including the null character, as shown below

Initialisation of Strings

1.If you want to initialise single character constant use single quotes {' '}.

Example : char c[5]={'a', 'b', 'c', 'd'};

2.If you want to initialise a string variable by assigning all characters collectively we need to use double quotes {" "}.

Example : char c[5]={" abcd"};

The initialisation for the above to examples will be as shown below 

Reading and printing strings :

We can read and print strings in c language in two ways

1.Using the formatted input output functions[printf( ) and scanf( )].

2.Using non formatted input output functions [gets( ) and puts( ) ].

1.Using the formatted input/output functions

2 formatted functions:

a) Formatted input function:  scanf( )

b) Formatted output function: printf( )

a) scanf( ) 

  •  The scanf() function reads strings from the keyboard and stores them in variables by using %s format specifier.
  •  scanf() function read strings which do not have any white spaces, that is, a white space or
  • a blank space in the string will terminate the reading of a string. To read a string with white space gets() function is used.
  • Syntax : scanf: (scanformat)
  • %s is the format string/ control string for string.
  • %s removes all white spaces present before the string is removed.
  • Copied string is terminated by “\0”.

 b) printf( )

  • The printf( ) function along with the %s format specifier prints a string stored in charactered array to the console.
  • We can use the printf() function to display the string data with the help of two ways.
  • Either we can pass the string data directly within the printf function or we can store it in an array and then print it.
        Example : printf("Hello") ;
                                    or 
                     char str[]={"Hello"}; 
                     printf("%s", str) ;


Program to read and write string using formatted input/output


#include <stdio.h>
{
   char name[20];
    printf("Enter your name : ");
    scanf("%s", name);
    printf("Your name is %s.", name);
    return 0;
}

 Output : 
 Enter your name : Mars
 Your name is Mars. 


2. Using non formatted input output functions


There are 2 non formatted input/output functions
a)Unformatted input function : gets( ) 
b)Unformatted output function : puts( ) 

a) gets( ) 

The gets( ) function is similar to the scanf( ) function the only difference is that gets( ) function can read the whole sentence, which is a combination of multiple characters while the scanf( ) function can read the character until a space or a new line character is encountered. 

b) puts( ) 
The puts( ) function on the other hand prints a string or a value stored in a variable to the console in the next line

Program to read and write string using non formatted input/output.

#include <stdio.h>
void main() 
{
  char name[20];
  printf("Enter your name :\n") ;
  gets(name) ;
  printf("\nWelcome ") ;
  puts(name) ;
  return 0;
}

 Output :
 Enter your name :
 MARS
 Welcome MARS
 

Post a Comment

0 Comments