Hsslive-xi-cs-siraj-09 String Handling and IO Functions
Hsslive-xi-cs-siraj-09 String Handling and IO Functions
C++ does not have a basic data type to represent string data. Using 'char' data type we can declare one
character at a time. A string can be defined as a character array that is terminated by a null character. The
null character '\0' is stored at the end automatically. അക്ഷരങ്ങളുടെ കൂട്ടമാണ് String. ഒരു String
ന്റെ
അവസാനത്തെ ക്യാരക്ടര് null character [\0] ആണ്. ഇത് കീബോഡില് എന്റര് കീ പ്രസ്സ് ചെയ്യുമ്പോഴാണ്
വരുന്നത്. string array യും initialize ചെയ്യാം. ഉദാ char code [5] = { 'g' , 'o', 'o', 'd' , '\0' }; So the
character arrays are declared one character longer than the largest string that can hold.
We can initialise a character array as follows.
char name[10]=”babu”; Here 10 memory locations will be allocated but the last 5 bytes will be left
unused.
char str[] = “hello world “; here size of the array is not specified
and so 12 bytes (11 bytes for string and 1for '\0' ) are needed.
To input a string and display
Memory allocation for the character array. #indlude<iostream>
memory allocation of a character array char c[6] using namespace std;
c[0] c[1] c[2] c[3] c[4] c[5] int main( )
M O H A N \0 {
char my-name[15];
Size of a char data type is one. A null character is stored at the end cout<<”Enter your name “;
of a string. cin>>my_name;
cout<<”Hello “<<my_name;
Input / Output operation of a string: Consider the program return 0;
In this example, on executing the program let the name be 'MAYA DAS ' . }
The output will be 'Hello maya' . This statement can store a string without
any white space (that is, only the first word).
A white space is treated as a separator of data. are two separate words. If we want store strings having
more than one word we can use functions like ' gets( ) ' ,. The syntax is ' gets(char_array_name) '. Here
we should include the header file 'cstdio. The 'gets( )' function helps to input of strings with white space.
C++ ല് data കള് input ചെയ്യുമ്പോള് white space (blank space ) , data യെ വേര്തിരിക്കാന്
ഉപയോഗിക്കുന്നു. string കള് input ചെയ്യുമ്പോള് cin എന്ന object ആണ് ഉപയോഗിച്ചതെങ്കില്
ആദ്യത്തെ വാക്കിന് ശേഷമുള്ള വാക്കുകള് സ്വീകരിക്കാന് കഴിയാതെ വരുന്നു. ഇതിന് പരിഹാരമായി cin ന്
പകരം gets( ) എന്ന function ഉപയോഗിക്കുന്നു. Out put ചെയ്യാന് puts( ) എന്ന function
ഉപയോഗിക്കുന്നു.
Similarly the function named 'puts( ) ' is used to output string data.
Syntax is puts(string_data);
The above program can be modify as follows
The output of the program will display in two lines. If we use the code To input a string and display
cout<<”hello”; cout<<name; instead of puts( ) function, the output will be #indlude<iostream>
the same line without a space. #include<cstdio>
More console functions on characters : using namespace std;
The header file ' cstdio ' must be included. int main( )
getchar( ) function: { char name[15];
it is a character function. It reads a character from the key board. cout<<”Enter your name “;
Eg: gets(name);
puts(“Hello “);
char ch;
puts(name);
ch=getchar( ); return 0;
cout<<ch; }
getch( ) function :
The typed (input) character will not be visible on the screen.
Eg:
ch= getch();
The typed variable is stored in the variable ch. Enter key need not be pressed to delimit the data.
getche( ) function :
[ get a character and echo it ] the typed character will be stored and echoed on the screen.
Eg: ch= getche();
putchar( ) function :
Char text[20];
cin>>text;
cout<<text;
A B C
a). getchar( ) i. String input 1. just a key stroke is enough a – iii – 4
b - iii – 1
b). getch( ) ii. String output 2. string having white spaces c–i–2
c). gets( ) iii. Character input 3. includes new line as the last character while spaces d – iv – 5
e – ii - 3
d). putchar( ) iv. Character 4. only the first character of the typed string is stored
output
e). puts( ) 5. if the parameter is 65, the output is A