0

Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter

For example
I say hi
Output should be
3 words
1 a
1h
2 i
1 s
1 y

#include <iostream>
#include <string>                     //include library for string processing

using namespace std;

int main(int argc, char *argv[])
{
    string line;
    int stringWordCount(string);
    
    cout << "Enter a string: ";
    getline(cin, line);
    
    cout << "Word Count: " << stringWordCount(line) << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


// counts the words in a string
int stringWordCount(string s)
{
    int i, count = 0;
    
    i = 0;
    while (i < s.length()) {
          while (isspace(s[i]))                           //skip white space
                i++;                             
          if (i < s.length()) {                           //found a word
             count++;
             while (!isspace(s[i]) && i < s.length())     //skip the word
                   i++;
          } // end if
    } // end while
 
   return count;
} // end stringWordCount

getting these warnings

Warning 1 warning C4018: '<' : signed/unsigned mismatch
Warning 2 warning C4018: '<' : signed/u

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by jencas
0

std::string.length() returns a size_t (unsigned int) and you are comparing it with an int. Change variable i from int to size_t to fix the warning.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.