characters and cstrings inputoutput Write a program to perfo

(characters and c-strings input/output).

Write a program to perform the following:

1.Read a character from the input and then print it out (using cin.get(ch) and cout.put (ch ))

2.Read and print one a word (using cin>> and cout<<)

3.Read a line of text that has a maximum of 80 characters and then print it out.

4.Read a number of lines and then print them out until the user triggers EOF.

Notes:

In Windows, you can trigger EOF by pressing Ctrl+Z at the beginning of a line ( Enter Ctrl+Z Enter).

In MAC OSX and Linux, you can trigger EOF by pressing Ctrl+D at the beginning of a line ( Enter Ctrl+D Enter).

Solution

1.Read a character from the input and then print it out (using cin.get(ch) and cout.put (ch ))

#include <iostream> // std::cin, std::cout


int main () {
char ch;

std::cout << \"Enter the character: \";
std::cin.get(ch); // get c-string
std::cout<<\"The character is:\";
std::cout.put(ch);

return 0;
}

Output:

Enter the character: K

The character is: K

2.Read and print one a word (using cin>> and cout<<)

#include <iostream> // std::cin, std::cout

using namespace std;
int main () {
char ch[255];
cout<<\"Enter the word:\";
cin>>ch;
cout<<\"The word is:\"<<ch;
return 0;
}

Output

Enter the word:Kartheek                                                                                                            

The word is:Kartheek

3.Read a line of text that has a maximum of 80 characters and then print it out.

#include <iostream>
using namespace std;

main()
{
char ch[11];

while (!cin.fail()) { // Loop Checks till fails
cout << \"Enter some text: \";
cin.getline(ch, 80); // Reads and prints line upto 80 characters
cout << \"You entered \'\"<< ch << \"\'\" << endl;
}
}

Output

Enter some text: kartheek is good boy, although he does not like good thoughts to be done. so he choosen good day biscuits to eat, hence everything is good in his life   

You entered \'kartheek is good boy, although he does not like good thoughts to be done. so he\'

4.Read a number of lines and then print them out until the user triggers EOF.

#include <iostream.h>
#include <fstream.h>

int main()
{

ifstream iFile(\"Sample.txt\");
// Sample.txt has integers, one per line

while (!iFile.eof()) // Reads till end of file
{
   char x[255];
   iFile >> x;
   cerr << x << endl;
}

return 0;
}

Sample.txt:

kartheek
patnaik
chandra

Output:

kartheek

chandra

patnaik

(characters and c-strings input/output). Write a program to perform the following: 1.Read a character from the input and then print it out (using cin.get(ch) an
(characters and c-strings input/output). Write a program to perform the following: 1.Read a character from the input and then print it out (using cin.get(ch) an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site