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

In C coding: 1) Prompt the user to enter a sentence 2) read in AT MOST 80 charac

ID: 3811559 • Letter: I

Question

In C coding:
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

Include 2 test cases and check if parseInput is setting the pointers properly.

The given program code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define BUFFERSIZE 81
Int parseInput (char userInput[], char // continue the rest
// continue the code
return 0;
}
Int main (int argc, char *argv[]) {
char userInput[BUFFERSIZE];
char *ptrsToChars[BUFFERSIZE];
// read in user input here
//call parseInput
// write code to display result
return 0;
}

Explanation / Answer

Below is the program to implement the problem given.
Most of the things are self explanatory.
Thigs to note are as below:
1.fgets second input as the number of characters to read, which for the question is 80.
2.To get the starting address of every word in the sentence, iterate through the sentence and check if we have a space if yes, add the address to the Array.At the same time we can count the number of sentences (using variable i).
3.Lastly print the starting characters of all the words.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define BUFFERSIZE 81
int parseInput (char userInput[], char* ptrsToChars[])
{
   int i = 1;
   char* ptr = userInput;
   ptrsToChars[0] = ptr;
   while(*ptr != '')
       (*ptr != ' ') ? ptr++ : (ptrsToChars[i++] = ++ptr);
   return i;
}
int main (int argc, char *argv[])
{
   char userInput[BUFFERSIZE];
   char *ptrsToChars[BUFFERSIZE];
   int i = 0;
   printf("Enter a sentene: ");
   fgets(userInput, 80, stdin);
   int numofwords = parseInput(userInput, ptrsToChars);
   printf("Number of words in the sentence = %d ",numofwords);
   while(i < numofwords)
       printf("%c ",*ptrsToChars[i++]);
   return 0;
}