Enter your favorite sentence and then search for a letter in that sentence. In t
ID: 3761619 • Letter: E
Question
Enter your favorite sentence and then search for a letter in that sentence.
In the main of your program you should declare an array of type char that is large enough to hold a fairly long sentence. (100 should be good.)
Then you have a function where you ask the user to type in their favorite sentence – of any length up to and including 100 characters. Remember that your program is NOT reading the whole sentence at once. Instead you are reading one character at a time with a scanf, in the way that you have read single characters in the past, but inside a loop that takes in the whole sentence. (The character you read into each time will be an array element.) The user is to mark the end of their sentence with a ‘.’ or a ‘!’. (Without the single quotes of course.) This becomes your sentinel value to control the loop. Since arrays are passed by address all the time, the original array declared in the main will be directly filled by this function, BUT you also have to return to the main the number of characters in the sentence. The easiest way to do this is with a return statement at the bottom of the function, which means that the return type of the function will be an int. You will need to store the value returned in an integer in the main.
Now ask the user to enter their favorite character. You will send this, as well as the sentence that is now stored in the array in main, to a second function which returns 1 if their character appears in their particular sentence, or 0 if it does not. The loop should be controlled by passing the length of the sentence (i.e. numUsed) to the function via passing method #1. (No & and no *.) This function will also have a return type of int.
If it does appear in the sentence the array, the number-of-characters and the character in question are sent to a third function (again with no & and no *) which returns how many times that character appears in the array. (This function also has a return type of int.)
The results of these two functions are output to the user in the main.
Sample dialog:
Please enter your favorite sentence
I like avocadoes very much!
Which character would you like to search for?
z
I’m sorry that character is not in your sentence.
Please enter your favorite sentence
I like avocadoes very much!
Which character would you like to search for?
e
Yes, that character is in there.
It appears 3 times.
*Bold indicates text that the user has typed in
Your program does not have to handle upper and lower case situations (where the user enters a different case for their search letter than they enter when they typed the letter in the sentence, such as happened with the i/I in my sentence), but if you would like to handle it you can do so using the tolower(c) or toupper(c) function from the ctype.h library.
Explanation / Answer
#include <stdio.h>
int read(char ch[])
{
int i=0;
char temp;
for(i=0;;i++)
{
scanf("%c",&temp);
if(temp=='.' || temp=='!')
{
break;
}
ch[i]=temp;
}
return i;
}
int search(char ch[],int len,char se)
{
int i;
for(i=0;i<len;i++)
{
if(ch[i]==se)
{
return 1;
}
}
return 0;
}
int cnt(char ch[],int len,char se)
{
int i,count=0;
for(i=0;i<len;i++)
{
if(ch[i]==se)
{
count++;
}
}
return count;
}
int main(void) {
// your code goes here
char ch[100];
int len;
len = read(ch);
printf("Please enter your favourite character: ");
char se;
scanf("%c",&se);
int temp = search(ch,len,se);
if(temp==1)
{
printf("Yes, that character is in there. ");
int cr = cnt(ch,len,se);
printf("It appears %d times. ",cr);
}
else
{
printf("I’m sorry that character is not in your sentence. ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.