944,026 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 15428
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 24th, 2006
0

Counting Spaces in a string

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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 ] );
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SiliconSpec is offline Offline
7 posts
since Mar 2006
Mar 24th, 2006
0

Re: Counting Spaces in a string

Pls try this code
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 13
Solved Threads: 2
Light Poster
beuls is offline Offline
40 posts
since Jan 2006
Mar 24th, 2006
0

Re: Counting Spaces in a string

Thanks man. I enter the string in, hit enter and it closes the program down?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SiliconSpec is offline Offline
7 posts
since Mar 2006
Mar 24th, 2006
0

Re: Counting Spaces in a string

Quote 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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2006
0

Re: Counting Spaces in a string

Awesome im slowly getting there! Thanks for the help thats wicked. Heres what I have shortened it down to.
C++ Syntax (Toggle Plain Text)
  1. for ( counter = i; input[ counter ]!= '\0'; counter++)
  2. {
  3. if (input[ counter ] == ' ' && input[ counter + 1 ] != ' ')
  4. spaces++;
  5. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SiliconSpec is offline Offline
7 posts
since Mar 2006
Mar 24th, 2006
0

Re: Counting Spaces in a string

[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.
Reputation Points: 13
Solved Threads: 2
Light Poster
beuls is offline Offline
40 posts
since Jan 2006
Mar 24th, 2006
0

Re: Counting Spaces in a string

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Mar 24th, 2006
0

Re: Counting Spaces in a string

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2006
0

Re: Counting Spaces in a string

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 24th, 2006
0

Re: Counting Spaces in a string

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can you help my hw???
Next Thread in C++ Forum Timeline: Declaring an array of records in MC++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC