Operation on strings
In c/c++ , the character datatype ( char ) stores a single character ( like – ‘h’ , ‘j’ , ‘d’ etc. ) but if the character variable is declared as string ( like char ch[10] ) , then , it can store words ( like “hello there ”, “this is the example of string ” , etc ) .
Note :-
char data are single character and always enclosed within
single quotes (like ‘a’ , ‘b’ , ‘c’ ,etc) .
string data are single or multi-character and always
enclosed within double quotes (like “a” , “abbc” , “hello there” , etc ) .
Example :-
char ch ; /* this is char type and store single character
like ‘a’ , ‘b’, ‘c’ etc , and enclosed within single quotes. */
char ch[10] ; /* this
is string type and store multiple character like “abc”, “this is the string” etc
, and enclosed within double quotes. */
The string always terminates with a null character i.e. ‘\0’,
like if we store a string as “hello” then internally it will be stored like “ hello\0
” and its size will be 6 bytes, similarly , “ this is string ” will be stored like “ this is string\0 ” and
its size will be 15 bytes.
Now if we use cin>> to input values in the string , then the major drawback of cin>> is it does not store spacebar value like if you enter a name like “ Pankaj Tripathi ” , then the space between Pankaj and Tripathi will not be input by cin>> and only Pankaj will get stored in the string .
So to overcome this problem , various inbuilt functions are defined below.
String Functions get() , put() and gets() , puts() and getline() , write()
get() function is similar to cin and is used to input a single character into a char variable.
put() function is similar to cout and is used for output purpose do print or display the variable information.
Syntax :
char ch ;
cin.get(ch) ;
cout.put(ch) ;
The gets() function is used to accept ( or input ) the string of characters entered and places them in the string variables .
The puts() function is used for output purpose of string. It is used to print the string.
The getline() function is similar to gets() function but it handle line-oriented input.
The write() function is similar to puts() function but it handle line-oriented output.
Syntax:
char ch[20], dh[20] ;
gets(ch) ;
puts(ch) ;
cin.getline(dh,20) ; /* i.e. cin.getline (name_of_variable,size) ; */
cout.write (dh,20) ;
Write a program to input a sentence from user and count the number of letters(including spaces between the words) in the sentence.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main() {
clrscr();
char sentence[500] ;
cout<<”\n Enter the Sentence - ” ;
gets(sentence) ;
int counter = 0 ;
for ( int i=0 ; sentence[i]!=’\0’ ; i++ )
{ counter = counter+1 ; }
cout<<”\n Total number of letters = ”<<counter ;
getch() ;
}
Write a program to input a sentence from user and count the number of spaces in the sentence.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main() {
clrscr();
char sentence[500] ;
cout<<”\n Enter the Sentence - ” ;
gets(sentence) ;
int counter = 0 ;
for (int i=0 ; sentence[i]!=’\0’ ; i++ )
{ if (sentence[i]==’ ’)
{ counter = counter+1 ; }
}
cout<<”\n Total number of letters = ”<<counter ;
getch() ;
}
Write a program to input a sentence from user and count the number of letters and spaces separately of the sentence.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main() {
clrscr();
char sentence[500] ;
cout<<”\n Enter the Sentence - ” ;
gets(sentence) ;
int letter_counter=0, space_counter=0 ;
for ( int i=0 ; sentence[i]!=’\0’ ; i++ )
{
if(sentence[i]==’ ’)
{ space_counter+=1 ; }
else
{ letter_counter+=1 ; }
}
cout<<”\n Total number of letters = ”<<letter_counter ;
cout<<”\n Total number of spaces = ”<<space_counter ;
getch() ;
}
0 comments:
Post a Comment
Please give your valuable suggestions