Thread: Count number of uppercase words in array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    36

    Count number of uppercase words in array

    We are going over csting, pointers, and references. When writing my programs, I usually start off really basic then work my way up to completion. So what I have here is basically the skeleton and my logic of what the final product should somewhat look like. Again, I need to count the # of words which begin w/ an uppercase letter only.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    { 
    const int max = 50;
    char message [max], ch;
    
    cout << "enter string \n";
    cin.getline(message, max);
    cout << "Msg is: \n" << message << endl;
    
    for (int i=0; message[i] !='\0'; i++)
    {
    ch = message [i];
    
    if (isupper (ch))
    cout << "upper, the word is "<< ch << endl;
    else if (islower (ch))
    cout << "lower, the word is " << ch << endl;
    }
    		return 0;
    }
    My main issue is trying to figure out when a word begins/ends.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by stevedawg85
    My main issue is trying to figure out when a word begins/ends.
    Perhaps "words" are text delimited by "spaces"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My main issue is trying to figure out when a word begins/ends.
    What do you define as a word in the first place?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    words:
    Apple, GDFF3423, T, FrAnkiE

    As long as the first letter is uppercase, it is a word. And how can i signify the end of a word? I have an idea to end when white space is first encounter but I'm not sure exactly how to write that out.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that (cin >> x) reads into x and skips whitespace between 'words'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    Yah I know, thats why I'm using the array. I also asked my prof and he gave me this little hint:
    Code:
    while (!isspace(yourArrayPtr) && yourArrayPtr != '\0')
    {
    Your code
    }
    I belive this answer my question, does it :P?

    If it does, all I am missing is the "yourarrayptr" How can I tell the user to input a string of unknown amount of chars and then use it as a pointer

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > If it does, all I am missing is the "yourarrayptr"
    Just replace it with

    message[i]

    and see if it makes more sense.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    I tired replacing it like you said but I still cant quite figure it out. I somewhat got the program running using this bootleg version, lol:

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    void array (char b[], int *bmax);
    int main ( )
    {							//Open main
    int max=56;
    char *b = new char [ max];
    b=&b[0];
    
    cout << "enter string\n";
    
    cin.getline(b, max);
    
    cout << "displayed: "<< endl;
    for (int i=0; b [i]!= '\0'; i ++)
    {					//open for
    	cout <<  *(b +i);
    }		// end for
    array (b, &max);
    return 0;
    }						// end main
    void array (char b[], int *bmax)
    {
    int count =0;
    for (int i=0; i < *bmax; i ++)
    	{       //Start for
    if (   (  isupper(*(b+i)) && isspace (*(b+i-1))) ||  (  (isupper(*(b+i))== isupper (b[0])) && isupper(*(b+i)) && *(b+i)== b[0] )    )
    {		//Start if
    count ++;
    }			// close if
    }		//end for
    delete []b;
    cout << count;
    }				//close void
    I know there is an easier way to write this but this seems to get the job done :P. Well, If you would like to help me reduce this to something simpler, you help is welcome, but here is my new concern:
    How do i get the words which begins w/ a captial letter to be displayed along with the total numbers. (EX: The doG is HAPPy****2 words are The, HAPPy.

    Thnx for your help and time

  9. #9
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Use the space-button!!!
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    Can someone please help me. This assignment is due midnight tonight. All I'm doing is asking for help. I have tried so many things, this is like my 3rd version:

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    void array (char b[], int *bmax);
    
    int main ( )
    {							//Open main
    int max=56;
    char *b = new char [ max];
    b=&b[0];
    
    cout << "enter string\n";
    	cin.getline(b, max);
    array (b, &max);
    return 0;
    }						// end main
    
    void array (char b[], int *bmax)
    {
    	int count =0;
    	char ch;
    
    	ch= *(b);
    
    	for ( *(b); ch < *bmax; b++)
    	while (ch != ' ' && ch != '\n')
    		{
    			if (isupper (ch))
    			{
    			count ++;
    			cout << ch;
    			}
    			break;
    		}			// close if
    
    	delete []b;
    	cout << "\n" <<count;
    }				//close void
    *edited code*
    Last edited by stevedawg85; 04-09-2006 at 05:17 PM.

  11. #11
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    void array (char b[], int *bmax);
    
    
    const int MaxSize = 60;
    
    int main ( )
    {//Open main
    
            char *buffer = new char[MaxSize];
    
            cout << "Please enter the string to evaluate:\n";
    
            cin >> noskipws;
            cin.getline(&buffer[0],MaxSize);
    
            cout << "Message entered is: " << buffer << "\n";
    
            int upcase_words = 0;
            for (int i=0; i<MaxSize && buffer[i]!='\0'; i++) {
    
                    if (i==0 && isupper(buffer[i])) {
                            upcase_words++;
                    } else if (buffer[i-1]==' ' && isupper(buffer[i])) {
                            upcase_words++;
                    }
            }
    
            cout << upcase_words << " words start with uppercase\n";
    
    }//close void
    Here's a working version of what you want to achieve, although it just counts the words, it does not output which words are. Plus, the code is not that good but it was written in a hurry.

    [edit]
    this is like my 3rd version
    Man i must tell you i feel really dumb for taking the time to write this code for you, if i had spotted this line before you can rest assured you would not have this code. Nevertheless i decided to leave the code, i just really hope that you take the time to try and LEARN SOMETHING from this as from what concerns me you won't get any other homework done for you...

    [/edit]
    Last edited by LinuxCoder; 04-09-2006 at 05:52 PM.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    THANKS SO MUCH BRO!!!!!! you dont know how much I appreciate this. Hopefully I can figuire out the other part. THANKS AGAIN!!!!!!!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can someone please help me. This assignment is due midnight tonight. All I'm doing is asking for help. I have tried so many things, this is like my 3rd version:
    Sometimes you just need to take a step back and ask what you really want to do. Since you probably have submitted your assignment, I suppose it is safe to show you how your homework might have been done

    The way I see it, this is what you want to do:
    1. Read a line.
    2. Print what was read (i.e. the line).
    3. Count number of capitalised words in this line.
    4. Print the count and the capitalised words.

    #1 and #2 can be solved by using a C++ string and getline().
    #3 and #4 can be solved by using a (dynamic) array to store the words.
    Extracting the words from the string is perhaps the most difficult part, but formatted input with std::istream (e.g. std::cin >> x) pretty much solves that for you.
    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    #include <sstream>
    #include <cctype>
    
    typedef std::string WordType;
    typedef std::vector<WordType> WordListType;
    
    int main() {
    	// Get the input sentence
    	std::cout << "Enter a sentence: ";
    	std::string sentence;
    	std::getline(std::cin, sentence);
    	std::cout << "Sentence entered is: " << sentence << "\n";
    
    	// Pass the input sentence to a stringstream
    	std::stringstream wordstream(sentence);
    
    	WordListType words;
    	WordType word;
    
    	// Extract words from the wordstream using formatted input
    	while (wordstream >> word) {
    		// Place capitalised words into word list
    		if (std::isupper(word[0])) {
    			words.push_back(word);
    		}
    	}
    
    	std::cout << "Number of capitalised words: " << words.size();
    
    	// Print list of capitalised words unless there are none
    	if (!words.empty()) {
    		std::cout << "\nThe capitalised words are: ";
    		for (WordListType::const_iterator i = words.begin(); i != words.end(); ++i) {
    			std::cout << *i << " ";
    		}
    	}
    
    	std::cout << std::endl;
    	return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 08:05 AM
  2. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 11:45 AM
  3. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM