Help with a simple C++ program

This is a discussion on Help with a simple C++ program within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, this is my first post in some time.. I need to create a simple C++ program that will ...

  1. #1
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30

    Help with a simple C++ program

    Hey everyone, this is my first post in some time.. I need to create a simple C++ program that will count the number of words in an inputed sentence. I will admit that I am completely lost on how to do this..

    I know posting questions to homework problems when looking for the exact answer or program as a reply is frowned upon, and please understand that is not what I am looking for. I simply wish for someone to point me in the right direction, or as to suggest what method, command, or function to use in my C++ program to count the number of words in an inputed string.

    Thanks to all who who help! =)

    Code:
    #include <iostream>
    #include <string.h>
    
    char sentence;
    
    int main()
    
    {
    
    std::cout << "Please enter a sentence and press Enter: ";
    std::cin >> sentence;
    
    
    return (0);
    }
    Last edited by otchster; 06-21-2007 at 09:26 PM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    For starters:
    Code:
    #include <iostream>
    //#include <string.h>     NO!
    #include <string>
    
    //char sentence;    Globals are evil
    
    int main()
    {
    std::string input;
    
    std::cout << "Please enter a sentence and press Enter: ";
    //std::cin >> sentence;   This will stop with whitespace
    std::getline(std::cin, input);   //reads a line of cin into 'input'
    
    return (0);
    }
    Now to count. You can't count words, but, after all, what is a word (in a string)? It's a bunch of crap bookended by spaces. In general, a sentence with n spaces has n+1 words. So, what kind of algorithm could count whitespaces?
    Code:
    std::count(iterator,iterator,object_type);
    So...
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm> //for std::count
    
    int main()
    {
    std::string input;
    
    std::cout << "Please enter a sentence and press Enter: ";
    std::getline(std::cin, input);
    
    std::cout << ( std::count(input.begin(),input.end(),' ') + 1 ) << std::endl;       //Look from the beginning of the string to the end, counting the occurrences of the char ' '. 
    
    return (0);
    }
    Last edited by CodeMonkey; 06-21-2007 at 10:05 PM. Reason: +1
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    A few days ago I made a function that splits a string into a vector of words, also handles extra spaces nicely:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    std::vector<std::string> split(std::string text) {
      //hold the words in a vector of strings
      std::vector<std::string>args;
      if (text.length()==0) return args;
      //remove leading whitespace
      int firstnsp=text.find_first_not_of(" ");
      text=text.substr(firstnsp);
      //remove trailing whitespace
      int lastnsp=text.find_last_not_of(" ");
      text=text.substr(0,lastnsp+1);
    
      int i=0; //index to first character of current word
      int spi=0;//index of next space
      for(;;) {
        //find next space
        spi=0;
        for(int s=i;s<text.length();s++) {
          if (text[s]==' ') {spi=s;break;}
        }
        //break out of loop if no spaces were found
        if (spi==0) break;
        //add word to vector
        std::string newarg=text.substr(i,spi-i);
        if (newarg.length()>0) { //only add if the word has at least 1 character
          args.push_back(newarg);
        }
        //move up i to the character after the space
        i=spi+1;
      }
      //add final word to vector
      std::string lastarg=text.substr(i);
      if (lastarg.length()>0) {
        args.push_back(lastarg);
      }
    }
    
    int main() {
      std::string sentence="";
      std::cout << "Please enter a sentence and press Enter: ";
      getline (std::cin,sentence);
      std::vector<std::string> words=split(sentence);
      std::cout << "You entered " << words.size() << " words\n";
      return (0);
    }

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Cool. Using the stream's parsing capabilities is even more fun.
    Code:
    void f(const std::string & in, std::vector<std::string> & out)
    {
       out.clear();
       std::istringstream s(in);
       std::string buf;
       while(s >> buf) out.push_back(buf);
    }
    PS: Your function doesn't seem to return a value. It has to. When it does, the entire vector will have to be copied. Yikes.
    Last edited by CodeMonkey; 06-21-2007 at 10:46 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Quote Originally Posted by CodeMonkey View Post
    For starters:
    Code:
    #include <iostream>
    //#include <string.h>     NO!
    #include <string>
    
    //char sentence;    Globals are evil
    
    int main()
    {
    std::string input;
    
    std::cout << "Please enter a sentence and press Enter: ";
    //std::cin >> sentence;   This will stop with whitespace
    std::getline(std::cin, input);   //reads a line of cin into 'input'
    
    return (0);
    }
    Now to count. You can't count words, but, after all, what is a word (in a string)? It's a bunch of crap bookended by spaces. In general, a sentence with n spaces has n+1 words. So, what kind of algorithm could count whitespaces?
    Code:
    std::count(iterator,iterator,object_type);
    So...
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm> //for std::count
    
    int main()
    {
    std::string input;
    
    std::cout << "Please enter a sentence and press Enter: ";
    std::getline(std::cin, input);
    
    std::cout << ( std::count(input.begin(),input.end(),' ') + 1 ) << std::endl;       //Look from the beginning of the string to the end, counting the occurrences of the char ' '. 
    
    return (0);
    }
    Thank you so much, your post totally made me understand what I wasn't. Take care.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21