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

Recommended Answers

All 2 Replies

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.

Member Avatar for jencas

* obsolete *

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.