473,408 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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.

9 1399
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
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
donbock
2,426 Expert 2GB
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
donbock
2,426 Expert 2GB
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
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
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
9,208 Expert Mod 8TB
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

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

Similar topics

8
by: kchengso | last post by:
One of my printers in my printer.devicename call returns in lower case. Is it possible for me to change it to upper case? I am also wondering how, in the first place, this particular printer...
3
by: MHenry | last post by:
All the lower case "c" in my database table are now upper case "C" starting in January 2004. All prior data is fine. I just noticed this after installing a Microsoft update patch a couple of days...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
5
by: Mariame | last post by:
Hi Everyone, Is There a way to eliminate upper case, so the user could only write Lower Case or to transfer the Upper Case String to lower case in Visual Basic ???? Thx in Adv.
19
by: Eric Lindsay | last post by:
Should HTML 4.01 Strict markup be done in upper case or in lower case? I understand that HTML allows either upper or lower case. I also notice that XHTML apparently requires lower case. However I...
14
by: fniles | last post by:
In VB.NET 2005 can I check if a letter in a string is upper case or lower case ? For example: I have the following 2 lines: NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5...
4
by: titan nyquist | last post by:
Why does ToTitleCase not work if the case is already in upper case? I have to make my string lowercase, first, before I pass to to ToTitleCase to have it work. Titan
5
by: conan9 | last post by:
Hi folks,, I'm new here and having trouble to compile uppercase .... and here is my code: #include <iostream> #include <string> #include <iomanip> #include <algorithm>
6
by: lenniekuah | last post by:
Hullo Awesome Helpers, Thank you for helping me earlier. I am back with new problem. I am trying to covert a TEXT String into either Lower and Upper case characterS. (Upper case Eg. ALFRED instead...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.