Counting Spaces in a string

Reply

Join Date: Mar 2006
Posts: 7
Reputation: SiliconSpec is an unknown quantity at this point 
Solved Threads: 0
SiliconSpec SiliconSpec is offline Offline
Newbie Poster

Counting Spaces in a string

 
0
  #1
Mar 24th, 2006
Hey all. Im currently studying C++ and im having a little bit of trouble figuring out how to count spaces in a string. The program is to enter a full name with any amount of names and print the initials of the first name and print the last name. Im using Borland C++. I have a basic Pseudocode written out for it, here it is:

loop through input and count spaces1
copy first character to output
loop through name again
count spaces
if current character is space
add one to spaces2
increment loop counter
concat next character
concat ". "
when last space is reached
concat until end of input
end loop
print name 2

Im fairly fresh to C++ so im just struggling to figure to code out for this. Here is what I have so far, please dont shoot me down if it is way off

  1. char input[ 45 ];
  2. char output[ 45 ];
  3. int spaces;
  4.  
  5. cout << "Enter Your Full Name: ";
  6. cin.getline( input, 45 );
  7.  
  8. spaces = 0;
  9.  
  10. do
  11. { if (( input [ 45 ] == ' ' ))
  12. spaces++;
  13. }while ( strlen( input ) != 0 );
  14.  
  15. strcpy( output[ 45 ], input[ 0 ] );
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 40
Reputation: beuls is an unknown quantity at this point 
Solved Threads: 2
beuls beuls is offline Offline
Light Poster

Re: Counting Spaces in a string

 
0
  #2
Mar 24th, 2006
Pls try this code
  1. #include <iostream.h>
  2. #include <string.h>
  3.  
  4. void main()
  5. {
  6. char s[100];
  7. int j,ns=0,i;
  8.  
  9.  
  10. cout<<"Enter a string :";
  11. cin.getline(s,100);
  12. for (i=0;s[i]!='\0';i++)
  13. {
  14. if (s[i]==' ')
  15. ns++;
  16.  
  17. if(s[i]>=97 && s[i]<=123)
  18. {
  19. s[i]=s[i]-32;
  20. break;
  21. }
  22.  
  23.  
  24. }
  25. cout<<ns<<endl;
  26. for (j=i;s[j]!='\0';j++)
  27. {
  28. if (s[j]==' ' && s[j+1] != ' ')
  29. {
  30. if (s[j+1]>=97 && s[j+1]<=123)
  31. s[j+1]=s[j+1]-32;
  32.  
  33. }
  34. if (s[j]==' ' )
  35. {
  36. ns++;
  37.  
  38. }
  39. }
  40. cout<<"No of spaces :"<<ns<<endl;
  41. cout<<endl<<s;
  42.  
  43. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: SiliconSpec is an unknown quantity at this point 
Solved Threads: 0
SiliconSpec SiliconSpec is offline Offline
Newbie Poster

Re: Counting Spaces in a string

 
0
  #3
Mar 24th, 2006
Thanks man. I enter the string in, hit enter and it closes the program down?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Counting Spaces in a string

 
0
  #4
Mar 24th, 2006
Originally Posted by SiliconSpec
Thanks man. I enter the string in, hit enter and it closes the program down?
[joke]
I think that means it crashed.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: SiliconSpec is an unknown quantity at this point 
Solved Threads: 0
SiliconSpec SiliconSpec is offline Offline
Newbie Poster

Re: Counting Spaces in a string

 
0
  #5
Mar 24th, 2006
Awesome im slowly getting there! Thanks for the help thats wicked. Heres what I have shortened it down to.
  1. for ( counter = i; input[ counter ]!= '\0'; counter++)
  2. {
  3. if (input[ counter ] == ' ' && input[ counter + 1 ] != ' ')
  4. spaces++;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 40
Reputation: beuls is an unknown quantity at this point 
Solved Threads: 2
beuls beuls is offline Offline
Light Poster

Re: Counting Spaces in a string

 
0
  #6
Mar 24th, 2006
[QUOTE=SiliconSpec]Awesome im slowly getting there! Thanks for the help thats wicked. Heres what I have shortened it down to.





it's not counting spaces alone. pls read the question and the algorithm again.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 482
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 47
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Counting Spaces in a string

 
0
  #7
Mar 24th, 2006
All this use of '\0' is very C-like ..There is a much better way using C++ strings (And by the way, ignore the use of <string.h> <iostream.h> and void main() )
C++ strings have a size() function which tells you how many characters are in the string.
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6. std::string sentence("hello, I am a string with 7 spaces.");
  7. int spaces=0;
  8. for(int i=0; i!=sentence.size(); ++i)
  9. spaces+=( sentence.at(i)==' ');
  10.  
  11. std::cout << "number of spaces: " << spaces;
  12. std::cin.get();
  13. return 0;
  14. }

Note - sentence.at(i) is the C++ equivalent of sentence[i]. you can use either form, and your compiler won't complain, but the at() function is automatically range checked.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Counting Spaces in a string

 
0
  #8
Mar 24th, 2006
>All this use of '\0' is very C-like

You'll be surprised how many chumps come onto this board with questions about c++ claiming they are only allowed to use C syntax. It would appear their teachers teach c++ with the notion that it is fine and dandy to mix the two languages.

>C++ is a multi-paradigm language

This is probably the biggest irony for the language. Heh heh, you would never mix java with C/C++ why then is it apparently ok to mix C with C++. Pffft.

Incompetence, burn da teachers.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Counting Spaces in a string

 
0
  #9
Mar 24th, 2006
>why then is it apparently ok to mix C with C++.
If C++ supports the features then the only problem is with stuffy people who think that C++ should be pure...for some skewed definition of "pure". :rolleyes: For example, C-style strings are perfectly acceptable to the C++ standard. There's nothing wrong with using C++ to write low level code, provided you're doing so for the right reasons.

On the other hand, if someone uses a C feature in C++ that's unavailable in C++ (relies on implementation quirks) or has subtly different behavior (bug waiting to happen), you have good cause to burn them to a crisp because that has a direct impact on the quality of software.

I do agree that many teachers don't seem to know the language well enough to teach it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Counting Spaces in a string

 
0
  #10
Mar 24th, 2006
>for some skewed definition of "pure".

I like the skewed definition of pure... So sue me.

>There's nothing wrong with using C++ to write low level code...

Erm so why not just use C den? Tee he he

>I do agree that many teachers don't seem to know the language well enough to teach it.

ha ha.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC