Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Strings Write a program reading.c that reads from standard input according to th

ID: 3702174 • Letter: S

Question

Strings
Write a program reading.c that reads from standard input according to the user’s choice. The program
should ask “How would you like to read?” 1: character by character; 2: word by word;
3: line by line. Default: print “Invalid choice”. You should write the three procedures below.
- The procedure readCharByChar asks the user to enter 5 characters and read them (hint: using %c).
- The procedure readWordByWord asks the user to enter 5 words and read them (hint: using %s).
- The procedure readLineByLine should ask the user to enter 5 lines and read them (hint: using gets).
In each of the three procedures, the values read should be printed to the screen, each on a separate line
showing: input index, tab, unit (char, word, or line).

According to the user choice, your program uses a switch statement to call readCharByChar,
readWordByWord, and readLineByLine, respectively.
Make sure your code is properly indented, your variables have meaningful names, and macro definitions
are used when appropriate.

Explanation / Answer

PROGRAM

#include<stdio.h>

// Declare and implement readCharByChar() function

void readCharByChar()

{

char a[5]; // Declare Character array variable "a"

int i; // Declare integer variable "i"

printf(" Enter 5 characters: ");

for(i=0;i<5;i++) // Create for loop for read 5 characters

scanf("%c",&a[i]); // reading chracters using scanf() function

printf(" The 5 characters are ");

for(i=0;i<5;i++) // create for loop for display 5 characters

printf("%c ",a[i]); // display characters using printf() function

}

// Declare and implement readWrodByWord() function

void readWordByWord()

{

char a[100]; // Declare Character Array variable "a"

printf(" Enter 5 words: ");

scanf("%[^ ]",a); // reading words using scanf() function in this %[^ ] means reading words until enter key

printf(" The 5 words are ");

printf("%s ",a); // display words

}

// Declare and implement readLineByLine() function

void readLineByLine()

{

char a[200]; // Declare Character Array variable "a"

printf(" Enter 5 Lines and Press TAB and Enter Key then stop: ");

scanf("%[^ ]",a); // reading words using scanf() function in this %[^ ] means reading number of lines until press tab key

printf(" The 5 Lines are ");

puts(a); // display total lines

}

// Declare and implement menu() function return type integer

int menu()

{

int ch; // Declare integer variable for choice

// Display Menu options

printf(" 1: Character by Character");

printf(" 2: Word by Word");

printf(" 3: Line by Line");

printf(" How would you like to read? ");

scanf("%d",&ch); // read menu choice

return ch; // return user enter choice

}

int main()

{

int ch; // Declare integer variable "ch"

ch=menu(); // calling menu() function and receive return value into "ch"

switch(ch) // create switch case and accept choice "ch"

{

case 1: fflush(stdin); // This function is used to clear memory

readCharByChar(); // calling readCharByChar() function

break;

case 2: fflush(stdin); // This function is used to clear memory

readWordByWord(); // calling readWordByWord() function

break;

case 3: fflush(stdin); // This function is used to clear memory

readLineByLine(); // calling readLineByLine() function

break;

default: printf(" Invalid Choice");

}

return 0;

}

OUTPUT-1


1: Character by Character
2: Word by Word
3: Line by Line

How would you like to read? 1

Enter 5 characters: abcde

The 5 characters are
a b c d e

OUTPUT-2


1: Character by Character
2: Word by Word
3: Line by Line

How would you like to read? 2

Enter 5 words: This is C Programming Language

The 5 words are
This is C Programming Language

OUTPUT-3


1: Character by Character
2: Word by Word
3: Line by Line

How would you like to read? 3

Enter 5 Lines and Press TAB and Enter Key then stop:
This is a Testing Line 1
This is a Testing Line 2
This is a Testing Line 3
This is a Testing Line 4
This is a Testing Line 5


The 5 Lines are
This is a Testing Line 1
This is a Testing Line 2
This is a Testing Line 3
This is a Testing Line 4
This is a Testing Line 5

OUTPUT-4


1: Character by Character
2: Word by Word
3: Line by Line

How would you like to read? 4

Invalid Choice