Search words starts with Capital letters

Hi every one.
I have a problem when i try to solve the program that search the word begin with uppercase and print it into console and the times it apear in the string.
eg: I put in the string "This is my first topic. Please help me. Iam Bigstorm."
then the program will print
This 1
Please 1
Iam 1
Bigstorm 1

But my program not work corectly. please help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

int main(){
    int k=0;
    string text, name;
    cout<<"Put the string: \n"<< endl;
    getline(cin, text);
    int n= text.length();
    for(int i=0 ; i< n ; i++){
        if(isupper(text[i])!=0){
            for(int j= i; j< n ; j++){
                if(text[j]==' '||text[j]=='\0')
                    break;
                name[k] = text[j];
                k++;
            }

        }
        cout<< name;
    }
    return 0;
}
Last edited on
It was confusing reading your code Bigstorm so I took the liberty of re-writing it with some added and removed parts. From what I understand, you want the program to read words that start with capital letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    string line = "NULL", wordCheck = "NULL";
    int lineLength = 0;

    cout << "Input a line: ";
    getline(cin, line);

    lineLength = line.length();

    for(int i = 0; i < lineLength; i++)
        if(isupper(line[i]))
        {
            wordCheck = line[i];

            for(int j = i + 1; j < lineLength; j++)
            {
                if(ispunct(line[j]) || isblank(line[j]))
                {
                    i = j + 1;
                    break;
                }

                wordCheck += line[j];
            }

            cout << wordCheck << endl;
        }

    return 0;
}
Thanks you, but your program not work correctly when i try put the line.
This is the output


Input a line: Faebook and Facebook logo are the trademarks of Facebook Inc. All right are reversed.
Faebook
Facebook
Facebook
All
 


"Inc " not print.
Then i want to print the frequence each word appear.
Topic archived. No new replies allowed.