1) Prompt the user to enter a sentence 2) read in AT MOST 80 characters of the s
ID: 3705315 • Letter: 1
Question
1) Prompt the user to enter a sentence 2) read in AT MOST 80 characters of the sentence the user entered 3) call function parseInput that will go through the input looking for a space and create pointers to each character after the space 4) print out the number of words in the sentence (that was at most 80 characters) and the characters that the pointers reference
The idea:
User inputs: A long sentence that will get us started on the lab so that we can see what is really going on here
Your code should only read in A long sentence that will get us started on the lab so that we can see what is r
And build an array of pointers to characters count the number of words and print out each word via a pointer to the word: The 80 characters read were: A long sentence that will get us started on the lab so that we can see what is r There were 19 words and the starting letters of the words were: A l s t w g u s o t l s t w c s w i r
8 points for the test cases (one of the test cases is worth 4 points and checks if parseInput is setting the pointers properly) 2 points comments / headers
Can you please write the code in basic C
:)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFERSIZE 81 // 80 characters plus end of string
int parseInput(char userInput[], char *ptrsToChars[]){
// your code goes in here to find the spaces and count the words and put pointers to characters after the spaces into ptrsToChars
// when your function is done, ptrsToChars[0] should have address of first character in first word in userInput
// and ptrsToChars[i] should have the address of the first character of word i
return 0; // this should return the number of words in userInput
}
int main (int argc, char *argv[])
{
char userInput[BUFFERSIZE]; // Buffer to hold user input
char *ptrsToChars[BUFFERSIZE]; // array to hold pointers to characters in the above buffer
// read in user input here
// call parseInput here
// write code to display results from parseInput
return 0;
}
Explanation / Answer
Screenshot
---------------------------------------------------------------------------------------------------------------------------
Program
//Header files
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Memory size of the array
#define BUFFERSIZE 81 // 80 characters plus end of string
//Function for finding count of words and first character of each word
int parseInput(char userInput[], char *ptrsToChars[]){
// local Variable
int i,count=1,j=0;
//First word first character in pointer
ptrsToChars[0]=&userInput[0];
//Increment variable to point next word first character
j++;
//Loop for finding word count first characters of the word
for (i = 0;userInput[i] != '';i++)
{
//Find space between words
if (userInput[i] == ' '){
//Put first character
ptrsToChars[j]=&userInput[i+1];
//Increment count according the words
count++;
j++;
}
}
//Return count of words
return count;
}
//Main function
int main(void) {
//Variables declaration
int i,counter;
//Arrays for string and first character of the word
char userInput[BUFFERSIZE],*ptrsToChars[BUFFERSIZE];
//Prompt user for text
printf("Enter a sentence ");
//Read text
scanf("%[^ ]s",userInput);
//Count of words getting by calling parseInput function
counter=parseInput(userInput,ptrsToChars);
//print count of words
printf(" count of the words in the text=%d ",counter);
//Loop to print first charcters of the words
for (i=0;i<counter;i++){
printf("%c ",*ptrsToChars[i]);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.