Write a program that accepts a series of strings from the keyboard, and prints t
ID: 3626125 • Letter: W
Question
Write a program that accepts a series of strings from the keyboard, and prints to the screen only those that start with a certain character. The string, which may contain spaces, but will not be longer than 30 characters, should be printed right justified in a field width of 35.You should use the following functions:
-- a function that gets the character
-- a function gets the string
-- a function that takes the character and the string as arguments and returns a boolean true if the string starts with the character (and false otherwise).
-- a function that returns a boolean true if the user wants to enter another string
Your program should
get the character
Then, in a loop,
get the string,
print it if it starts with the character
ask the user if another string will be entered
When there are no more strings, print a count of the number of strings entered, and the number of strings printed.
General Programming Requirements:
-- don’t hardcode constants
-- As we will discuss in class, it may be necessary to clear the keyboard buffer of any left over newline characters, depending on how you decide to read the required data in each of these problems. The issue is that scanf() leaves a new line in the buffer, while other string reading functions, such as gets(), stop when they see a new line character. So if a new line character is the first character in the buffer when gets() is called, it thinks it is done, and doesn’t wait for new input. The statement
fflush(stdin);
will remove any characters, including the new line character, from the buffer, leaving it ready to accept the next input.
I will appreciate it if I get the response in C Programming. Thank you.
Explanation / Answer
#include #include #include void get_ch(); void get_str(); int more (); int match(); int ch1,ch2; char ch,str[30]; void main() { clrscr(); do { fflush(stdin); get_ch(); fflush(stdin); get_str(); fflush(stdin); ch1=match(); if(ch1==1) { printf("%35c"); puts(str); printf(" "); } else printf("Match not found"); fflush(stdin); ch2=more(); }while(ch2==1); } void get_ch() { printf("Enter a character:"); scanf("%c",&ch); } void get_str() { printf(" Enter a String:"); gets(str); } int match() { if(ch==tolower(str[0])) return 1; else return 0; } int more() { char another; printf(" Want to enter more(y/n):"); scanf("%c",&another); if(another=='y'|| another=='Y') return 1; else return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.