Question

Use the function isSeparator(c) above in writing a function that counts the number of words in...

Use the function isSeparator(c) above in writing a function that counts the number of words in a string. In your solution, consider different cases of the string, such as having few adjacent space characters, tab and newline characters. You may consider that words can be 1 or more alphanumeric character(s), an alphanumeric is either an alphabetical letter (lower or upper case) or a number Hint: The last character of a word is followed by a separator, a new line, or a null character.

write in c programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.


Here is the completed code for this problem. Let me know if you have any doubts or if you need anything to change.


Thank You !!

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

#include<stdio.h>

int countWords(char* ptr){
  
   int count=0,i;
   for (i = 0;ptr[i] != '\0';i++)
{
if ((ptr[i] == ' ' || ptr[i] == '\t' || ptr[i] == '\n' ) && (ptr[i-1] != ' ' && ptr[i-1] != '\t' && ptr[i-1] != '\n' ))
count++;
}
// when the sentence does not ends with a space and new line and tab
if(ptr[i-1] != ' ' && ptr[i-1] != '\t' && ptr[i-1] != '\n')
count++;
return count;
  
}

int main(){
  
   char* sentence="hi this is a very long\tsentence, \n\n\n\t\njust to test!! ";
   //char* sentence="hi";
   printf("Total words: %d",countWords(sentence));
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

LexiographicalSort.cpp indexOf.cpp countWords.cpp 1 #include<stdio.h> 3 int countWords (char* ptr){ am 5 int count=0,i; for (

Add a comment
Know the answer?
Add Answer to:
Use the function isSeparator(c) above in writing a function that counts the number of words in...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Read a string then count the number of words in the string. Need to count the...

    Read a string then count the number of words in the string. Need to count the number of white spaces that include single blank space ' ', tab \t, new line \n. If a non-white space character is followed by a white space or NULL character then it is a word.

  • Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array...

    Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array listing words and their counts in sorted order Function Description: Have you ever seen those fancy word-clouds based on the frequency of word occurrences in some text? Well the first step of making a graphic like that is to figure out how often the words in some text occur, which is exactly what this function will do. You will count the number of occurrences...

  • Java question: Create a class called ​Composition​. Read in all of the words in a file...

    Java question: Create a class called ​Composition​. Read in all of the words in a file called dictionary.txt​. Each word will be separated by whitespace (i.e. a space, a tab or a newline character). You must read in all words and determine if a word can be created by concatenating two other words. For example, the word racecar can be created by concatenating race and car. All such possibilities must be output to a file in this format: racecar: race...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Prompt the user and read in a string. Make your string large enough to hold 100...

    Prompt the user and read in a string. Make your string large enough to hold 100 characters. 2. Count the number of words in the string. A word is one or more non-blank characters separated by one or more blanks. My suggestion is to use a flag (a boolean variable) to indicate whether or not you are in a word. Then you know you have found a word when the flag indicates that you are in a word and the...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Write a program that reads a string entered at the command line and counts the number...

    Write a program that reads a string entered at the command line and counts the number of characters or words. A blank space should be used to delimit a word. Name the program StringCount and design it to be invoked using syntax as follows: StringCount –[c][w] user_string

  • IN JAVA: Write a spell checker class that stores a set of words, W, in a...

    IN JAVA: Write a spell checker class that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a Spell Check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, since it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT