By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,327 Members | 2,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,327 IT Pros & Developers. It's quick & easy.

How do i print all the words, starting with upper case, from a string?

P: 5
I`m a beginner and i`m trying to make a program that finds all the words that start with upper case in a sentence, and then list them one after another, separated. I'm only able to use :
strlen
strupr
strlwr
strcat
strcmp
strcpy
strstr

Ex:
Input: "The red car is owned by Michael."
Output: The,Michael,

What i`ve done so far ( please tell me where i'm wrong):


#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char a[100];
int i,n;
n=100;
cout<<"introduce word/sentence:";
cin.get(a,99);
cout<<"Printing words:";
for(i=0;i<=n;i++)

if (a[i]!= ' ' && a[i]!= '!' && a[i]!= '?' && a[i]!= '.' && a[i]!=',')

.............
return 0;

}


Second thought:


int main()
{
char a[100];
int i,n;
n=100;
cout<<"introduce word/sentence:";
cin.get(a,99);
cout<<" Printing words:";
for(i=0;i<=n;i++)
if(a[i>='A'&&a[i]<='Z')

.................
return 0;

}


(currently i`m working with codeblocks)
P.S: Sorry if i post this in the wrong place or if i offended someone with my english.
Oct 21 '15 #1

✓ answered by weaknessforcats

If you have a string like this:

"The red car is owned by Michael."

the words are separated by spaces and a period.

Write a beginning program:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char a[100] = "   The red car is owned by Michael.";
  4. }
  5.  
First, write a loop that starts at element 0 and stops at the end of the string. You can use strlen to see how long the string is.

Get this much working.

Next, inside the loop display each character of the string.

Get this working.

Next, change the loop to display the character only if not a space.

You should now see the words of the string all pushed together with a period at the end.

Next, surround the display of the non-space character with a new loop. The loop will display the character as long as it is not a space and not a period. Start the loop where the outer loop is positioned. When the inner loop exits, position the outer loop where the inner loop left off. Then display a \n to start the next word on a new line.

You should see all of your words displayed.

Finally, change the test to enter the inner loop when the character is not a space to when the character is not a space AND is in the range of 65 to 90. From the ASCII table characters in the range 65-90 are capital letters.

You should now see only the words that start with capital letters.

Share this Question
Share on Google+
9 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
If you have a string like this:

"The red car is owned by Michael."

the words are separated by spaces and a period.

Write a beginning program:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char a[100] = "   The red car is owned by Michael.";
  4. }
  5.  
First, write a loop that starts at element 0 and stops at the end of the string. You can use strlen to see how long the string is.

Get this much working.

Next, inside the loop display each character of the string.

Get this working.

Next, change the loop to display the character only if not a space.

You should now see the words of the string all pushed together with a period at the end.

Next, surround the display of the non-space character with a new loop. The loop will display the character as long as it is not a space and not a period. Start the loop where the outer loop is positioned. When the inner loop exits, position the outer loop where the inner loop left off. Then display a \n to start the next word on a new line.

You should see all of your words displayed.

Finally, change the test to enter the inner loop when the character is not a space to when the character is not a space AND is in the range of 65 to 90. From the ASCII table characters in the range 65-90 are capital letters.

You should now see only the words that start with capital letters.
Oct 21 '15 #2

P: 5
So it should look like this i think:

Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<strlen(a);i++)
  2.       if (a[i]!=' ')
  3.          cout<<a[i];
  4.       else
  5.          cout<<", ";
  6.    return 0;
  7. }
This displays all the words, with ", " between them...now where do i insert that loop to pick only the ones that start with upper case?

Also if you could give me an example would be great.
Oct 21 '15 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
You need a loop around the a[i]. This will display your words.

The code flow is:

loop through the string and find a non-blank character in the range 65-90. You have already coded the test for a non-blank character so just expand it to include AND in the range 65-90.

Now you drop to cout << a[i]. It is here you embed this in a loop to display the word. You already know when you enter this loop that the character is non-blank and in the range 65-90. All this loop does is display characters until it finds a space or a period.

Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<strlen(a);i++)
  2.        if (a[i]!=' ') <--here you add test for 65-90
  3.        for(......) <-- here you cycle loop if character is not a space or a period
  4.        {
  5.           cout<<a[i];
  6.        }
  7.        else
  8.           cout<<", ";
  9.    return 0;
  10.  
Post again to let me know how you are coming along.
Oct 21 '15 #4

P: 5
Expand|Select|Wrap|Line Numbers
  1.  for(i=0;i<strlen(a);i++)
  2.         if (a[i]!=' '&& (a[i]>=65 && a[i]<=90)) 
  3.  
  4.          for(i=1;i!=a[' '] || i!=a['.'];i++) 
  5.            if (a[i]!=' '||a[i]!='.')
  6.  
  7.            {
  8.               cout<<a[i];
  9.            }
  10.            else
  11.               cout<<", ";
  12.  
  13.    return 0;
  14. }
  15.  
when you get:♥ M i c r o s o f t . W i n d o w s . S y s t e m C o m p a
t i b l e , p r o c e s s o r A r c h i t e c t u r e = " x 8 6 " , p u b l i c
K e y T o k e n = " 6 5 9 5 b 6 4 1 4 4 c c f 1 d f " , t y p e = " w i n 3 2 "
, v e r s i o n = " 6 . 0 . 7 6 0 0 . 1 6 3 8 5 " S y s t e m D e f a u l t
C o n t e x t x 8 6 _ m i c r o s o f t . w i n d o w s . s y s t e m c o m p
a t i b l e _ 6 5 9 5 b 6 4 1 4 4 c c f 1 d f _ 6 . 0 . 7 6 0 0 . 1 6 3 8 5 _ n
o n e _ 4 9 a d c c b d e 8 1 6 9 a 0 3 M i c r o s o f t . W i n d o w s . I
s o l a t i o n A u t o m a t i o n l ♦☺ |♦ ☻ ♣ δ█I&♦╩☺☺

as outcome + beepings...
you know its bad

so where am i wrong? i know it isnt right here, second for:
Expand|Select|Wrap|Line Numbers
  1. for(i=a[i];i!=a[' '] || i!=a['.'];i++)
  2.         if (a[i]!=' '||a[i]!='.')
Oct 21 '15 #5

Expert 100+
P: 2,398
Which characters are considered part of a word and which separate the words?

An easy answer is that alphabetic characters are in words and all non-alphabetic characters separate words; however that will exclude candidate words like "L8", "field-of-view", "Dan's", gmail.com", "red_black", "etc.", etc. if you want any of these to count as words then you need a more complicated rule for distinguishing words from separators.
Oct 22 '15 #6

Expert 100+
P: 2,398
Can you add these to the list of allowable library functions?
isspace()
isalpha()
isupper()

If not, then you can make your own portable versions by exhaustively testing against all characters in each group. Comparing against ASCII codes is not portable because there are a few compilers that don't use ASCZiI.
Oct 22 '15 #7

P: 5
I need to modify the last for, to start when meets upper case or when its not a period or blank (i think i solved that) then it has to stop when meeting a space or a period and print everyrhing between...then it tests again if there is a space then again if it is an upper case and so on...i'l try to do that soon as i get to the computer ( i can only use what i said first )
Oct 22 '15 #8

P: 5
I introduced:

The car is owned by Michael. It is a Mercedes not a Bmw.
SO i came up with this:


Expand|Select|Wrap|Line Numbers
  1. i=0;
  2.       while(a[i]!=' ') {cout<<a[i];i++;}
  3.            cout<<",";
  4.            for(int j=i+1;j<strlen(a);j++)
  5.  
  6.            {
  7.             if(a[j]>='A'&&a[j]<='Z')
  8.                 while(a[j]!=' '){cout<<a[j];j++;}
  9.  
  10.                  }
  11.  
  12.  
  13.       return 0;
  14. }
console displays :

introduce word/sentence: The car is owned by Michael. It is a Mercedes not a Bmw.

printing words: The,Michael.ItMercedesBmw. ◄u╬ś▒ ░fA đ■"*" ─*" Ň░uZ→─d
♥ ─[┤u░fA 0*" ░ř⌂ *" ř►@ (*" 4׻u ░ř⌂ř►@ ☺ p☼0 └0 ****\*" Ň░u→↔
─■***▲▬u᧻u└0 ◄(u☺ ░ř⌂Ľ↕@ ☺ lţ├u ░ř⌂ď*" │:
(w ░ř⌂↨4ew ░ř⌂ *" ****]#w█ě` *" ć:(w↕@ ░ř⌂
↕@ ░ř⌂ Actx
Process returned 0 (0x0) execution time : 45.380 s
Press any key to continue.

So it shows the words starting with upper case, shows the first 2 the right way, but its messed up ater those...any ideas?
Oct 22 '15 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
So where are you checking to see if you have run off the end of the string? If you encounter a \0. you are at the end.
Oct 22 '15 #10

Post your reply

Sign in to post your reply or Sign up for a free account.