Ads 468x60px

Sunday 13 January 2013

String

More than one character is called as a string. Suppose we need to store a name on a variable, but we can only store a single character by declaring a char type variable so if we need to store a name then we have to use character array. 
char name[30];
Now we can store a name in it, but how can we take the input, cin will not accept space so we can't take input by cin for a sentence or some words, cin can be only used for a single word, we can use looping, we can also take input by using function gets or getline, this would be more easier. 
gets(name);
cin.getline(name,30)
In gets we have to pass the variable name as the parameter in gets function, this function is defined under the header file stdio.h, so if you are using it you must define the header file to avoid error. 

In getline the syntax is 
cin.getline(variable_name,size)

Every string is terminated with NULL ('\0') character, so if we are declaring a character array of size 30, we can store only 29 characters on it because the last space is reserved for NULL character. 

Lets Understand it with a program 
Write a program to find the length and reverse of a string. 
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

void main()
{
 clrscr();
 char string[30];
 int i,l=0;

 cout<<"Enter a string:\t";
 gets(string);

 for(i=0;string[i]!=NULL;i++)
 {
  l++;
 }

 cout<<"The Length is "<<l<<endl;
 for(i=l;i>=0;i--)
 {
  cout<<string[i];
 }
 getch();
}
Explanation of the Program 
Variable declares are:
One character array of size 30, two integer variables i for loop and l for counting length. 

At first user gets a message that enter a string and we took input from the gets function by passing the variable name as the parameter. 

Suppose we entered 
belal haque khan

Now for loop, i is initialized with 0, and then the conditions (string[i] != NULL) is checked which is true (string[0] = b and it is not equals to NULL), so because the condition is true control will come inside the loop and will increment l by 1, so the value of l was 0 and it will become 1, now the i will increment by 1 and will become 1, again the condition will checked which is true again and control will again come inside the loop and increment l by 1, this process will continue until the value of string[i] will not become NULL. 

Now the length is stored in l, we have printed the the length on the output screen now we need to print the reverse of the screen for this we have to start the array index from l (the length we calculated) and we have to print the each value from the back, for this we took the help of the loop. 

The loop is initialized with l (i = l) and l = 16 (calculated in the first loop), now the control will check the condition ( i >= 0 ) which is true (16 is greater than 0 ) the control will come inside the loop and print the value of string[i], the value of i is currently 16 so it will print the value of string[16]. Now the value of i is decremented by 1 and will became 15, again the control will check the condition which is true (15 is greater than 0) again control will come inside loop and will print the value of string[15], again the value of i will be decremented by 1 and again the control will checks the condition this process will repeat until the condition will not become false. 
Output of the Program
Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!

 
Related Posts Plugin for WordPress, Blogger...