Question

Write a program that accepts an input string from the user and converts it into an...

Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input string has 1 space between each word and starts with an alphanumeric character. Your program’s input and output format should match the example run below. • You may only use stdio.h • Save your code as prob4.c.

Example Run

> This is a string.

4 word(s) found.

Homework Answers

Answer #1

Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.

Please provide your feedback
Thanks and Happy learning!

prob4.c:

#include <stdio.h>
int str_to_word(char inputStr[], const int arraySize)
{
    char* wordList[100] = { NULL };
    int wordCount = 0;
    int wordStartPosition = 0;
    int i = 0;

    while (inputStr[i] != '\0' && i < arraySize)
    {
        if (inputStr[i] == ' ')
        {
            //Replace the space with '\0'(NULL) to indicate end of a word
            inputStr[i] = '\0';

            //This check is to avoid a single space been considered as a word
            if (i != wordStartPosition)
            {
                //Point the char* in the wordList[] array to the start of the current word
                wordList[wordCount] = inputStr + wordStartPosition;
                //Increment the word count
                ++wordCount;
            }
            //set the next word start postition
            wordStartPosition = i + 1;
        }
        ++i;
    }

    //For the last word
    if (inputStr[i] == '\0' && i != wordStartPosition)
    {
        wordList[wordCount] = inputStr + wordStartPosition;
        ++wordCount;
    }

    return wordCount;
}
int main()
{
    const int arraySize = 100;
    int wordCount = 0;
    char inputStr[arraySize] = { '\0' };


    printf("Enter a string: ");
    gets_s(inputStr, 100);
    //gets(inputStr);

    wordCount = str_to_word(inputStr, arraySize);

    printf("%d word(s) found.", wordCount);

    return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
. Write a function in python that accepts a sentence as the argument and converts each...
. Write a function in python that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
This is function.py def processString(string): # Implement this function This is # This should print 4.5...
This is function.py def processString(string): # Implement this function This is # This should print 4.5 3.0 processString("An example. Two") # This should print 4.4 5.3 4.5 processString("This is the first sentence. The second sentence starts after the period. Then a final sentence") Download function.py and complete the processString(string) function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per...
def processString(string): # Implement this function # This should print 4.5 3.0 processString("An example. Two") #...
def processString(string): # Implement this function # This should print 4.5 3.0 processString("An example. Two") # This should print 4.4 5.3 4.5 processString("This is the first sentence. The second sentence starts after the period. Then a final sentence") This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a function that accepts a line of text and a single letter as input (case...
Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the first character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter. Example: Input text = "When the SUN rises at dawn, the chicken flies into the window." Input letter = "t" Output = "The letter t has appeared 3...
//C++ Given a string S containing a number of substrings separated by spaces. Write a program...
//C++ Given a string S containing a number of substrings separated by spaces. Write a program to do the following: Count the number of substrings in S and print S with extra spaces deleted. Define a dynamic array of pointers (a pointer for each substring in S). Save a pointer to each substring S into the array. Hint: use strtok function.
3. Create a program which allows the user to swap individual words in an input string....
3. Create a program which allows the user to swap individual words in an input string. Given a string of input, your program should count and store the number of words in a 2D array. The user can then select the index of the words they want to swap. Your program should swap those two words and then print the entire string to ouput. • Use string prompt "> ". • Use indices prompt "Enter two indices: ". • Remove...
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the...
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the length of each array. The function should return void. Using pointer arithmetic to access the array elements, compute the element-wise product of the two arrays in-place (the result is stored in the first array). The function should return void. In main, read in two arrays of 10 values from the user following the I/O format seen in the example run below. On a new...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT