| | |
Counting Spaces in a string
![]() |
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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
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)
char input[ 45 ]; char output[ 45 ]; int spaces; cout << "Enter Your Full Name: "; cin.getline( input, 45 ); spaces = 0; do { if (( input [ 45 ] == ' ' )) spaces++; }while ( strlen( input ) != 0 ); strcpy( output[ 45 ], input[ 0 ] );
•
•
Join Date: Jan 2006
Posts: 40
Reputation:
Solved Threads: 2
Pls try this code
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <string.h> void main() { char s[100]; int j,ns=0,i; cout<<"Enter a string :"; cin.getline(s,100); for (i=0;s[i]!='\0';i++) { if (s[i]==' ') ns++; if(s[i]>=97 && s[i]<=123) { s[i]=s[i]-32; break; } } cout<<ns<<endl; for (j=i;s[j]!='\0';j++) { if (s[j]==' ' && s[j+1] != ' ') { if (s[j+1]>=97 && s[j+1]<=123) s[j+1]=s[j+1]-32; } if (s[j]==' ' ) { ns++; } } cout<<"No of spaces :"<<ns<<endl; cout<<endl<<s; }
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
Awesome im slowly getting there! Thanks for the help thats wicked. Heres what I have shortened it down to.
C++ Syntax (Toggle Plain Text)
for ( counter = i; input[ counter ]!= '\0'; counter++) { if (input[ counter ] == ' ' && input[ counter + 1 ] != ' ') spaces++; }
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.
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.
C++ strings have a size() function which tells you how many characters are in the string.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> int main() { std::string sentence("hello, I am a string with 7 spaces."); int spaces=0; for(int i=0; i!=sentence.size(); ++i) spaces+=( sentence.at(i)==' '); std::cout << "number of spaces: " << spaces; std::cin.get(); return 0; }
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.
>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.
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*
>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.
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.
>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.
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*
![]() |
Similar Threads
- How to remove spaces from a string (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: Can you help my hw???
- Next Thread: Declaring an array of records in MC++
| Thread Tools | Search this Thread |
ace_thread api array based binary bitmap borland c++ c/c++ calling char class classes code coding compile console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






