Wednesday, March 20, 2013

Program to use string (Character array)

Before we use string we should have an idea about "What is a String ??"
A string is just a character array and follows all the properties of array. If we declare an array with char datatype then it will be called a string.
Syntax:
char array_name[size/limit];

Here in above syntax you see, we are using char as datatype of the array. String also has size or limit for characters as an integer array has. String is used to store character input like name, addresses etc.
We can get a string input using scanf() function with %s format specifier and we can print the string with printf() function with the same format specifier.
But the problem with scanf() and printf() is that, scanf() function only reads string before space character and printf() also prints the string till it don't gets a space character. So we use two different functions gets() and puts() to solve this problem.

gets() is used to get string input and puts() is used to output it. These both functions override the above mentioned problem which we are having with scanf() and printf().

Here is a simplest string program which gets a character string and prints it-

Powered by Blogger.