简体   繁体   中英

How do you find the number of occurences of a certain word in a sentence when reading in the sentence character by character?

For example, if I wanted to find the number of times that the word "MY" appears in a user-inputted sentence, how would I do that? Is it even possible to do this if I'm reading in the sentence one character at a time with a while-loop?

Sample input would be something like: "My house is here"

My current output is: Number of words.........4 Number of uppercase letters.........1 Number of lowercase letters........12 Number of vowels.........6 Number of substring MY.........0

where number of substring MY should be 1.

Here's what I currently have:

#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;

bool isvowel(char c) {
        if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            return true;
        } else {
            return false;
        }
}

int main() {
    char c;
    int words = 0;
    int upperCount = 0;
    int lowerCount = 0;
    int vowels = 0;
    int my = 0;

    cout << "Enter a sentence: ";

    while (cin.get(c), c != '\n') {
        if (isupper(c)) {
            upperCount++;
        }

        if (islower(c)) {
            lowerCount++;
        }

        if (isspace(c)) {
            words++;
        }

        if (isvowel(c) == true) {
            vowels++;
        }

        if (c == 'M' || c == 'm') {
            if (c+1 == 'Y' || c+1 == 'y') {
                my++;
            }
        }
    }

    cout << left << "Number of words" << setfill('.') << setw(10) << right << words + 1 << endl;
    cout << left << "Number of uppercase letters" << setfill('.') << setw(10) << right << upperCount << endl;
    cout << left << "Number of lowercase letters" << setfill('.') << setw(10) << right << lowerCount << endl;
    cout << left << "Number of vowels" << setfill('.') << setw(10) << right << vowels << endl;
    cout << left << "Number of substring MY" << setfill('.') << setw(10) << right << my << endl;

    system("Pause");
    return 0;
}

This can be done in many ways, you almost have one. I will not give you the exact solution but you can try something like this: (written in Java)

// String sentence = "My house is here";
// word = "My"
private int getWordCount(String sentence, String word) {
    char[] charArr = sentence.toCharArray();
    String currWord = ""; 
    int count = 0;
    for(char c : charArr) {
        if(c != ' ') { currWord += c; } // if c is not space it gets appended to the current word
        else { 
            if(currWord.toLowerCase().equals(word.toLowerCase())) {
                count++;    
            }
            currWord = "";
        }
    }
    return count;
}
  1. Keep a track of the current string. If the current character is not a whitespace, append it to the string; else, the string becomes empty.
  2. For each string, you could compare it to the target string. This comparison will have O(n) complexity, where n is the length of the string.
  3. To optimise it further, you could build a trie for the target string. Since you're already processing one character at a time, the string matching could be done in O(1) instead of O(n) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM