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 :)

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 ] );

Pls try this code

#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;
	
}

Thanks man. I enter the string in, hit enter and it closes the program down?

[joke]
I think that means it crashed.

Awesome im slowly getting there! Thanks for the help thats wicked. Heres what I have shortened it down to.

for ( counter = i; input[ counter ]!= '\0'; counter++)
	{
		if (input[ counter ] == ' ' && input[ counter + 1 ] != ' ')
		spaces++;
   }

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.

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.

#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. 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.

>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. :sad:

>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.

>Erm so why not just use C den? Tee he he
Strong typing, of course. That's the single biggest selling point of C++ for a lot of programmers. It can also be convient to work primarily low level, but still take advantage of container classes and standard algorithms.

>It can also be convient to work primarily low level, but still take advantage of container classes and standard algorithms.


Oooooh I don't get it. :sad: Why would you want to use low level language syntax with c++ and it's container classes. I don't see the advantage. Stop confusing me now... :cry:

>I don't see the advantage.
Okay, write a C++ function that interfaces with an assembly program and see how much trouble the name mangling gives you. Then once you get that worked out, see how much trouble working with C++ objects on the assembly level gives you.

Or better yet, do the opposite. Interface an assembly subroutine with a C++ program and perform an operation that's trivial with a standard container. Keep in mind that the assembly subroutine is extremely likely to give you something low level, and you need to handle that.

>Okay, write a C++ function that interfaces with an assembly program and see how much trouble the name mangling gives you. Then once you get that worked out, see how much trouble working with C++ objects on the assembly level gives you.

I no dat silly ;) I'm saying is why can't u just do that all in C?

Why would you want to use low level language syntax with c++ when you can use all low level syntax in C and have it work fine and dandy with an assembly program.

What is the advantage of using c++ if it is so difficult to interface a function and work with c++ objects with assembly.

Again, why not just separate the two and be done with it? This is wat we are debating isn't it?... I was sumwhat confused by your last statement.

>I'm saying is why can't u just do that all in C?
Well, what if I'm using a map in the C++ function? It should be immediately obvious that to do the same thing in C would require either a non-standard library or a *lot* of time duplicating the effort of std::map. In my experience, non-standard libraries are more likely to cause extra work than rolling your own solution. So I ask you, why not just use C++ with the container that makes your job easy?

Let me compare lines of code, since that's more obvious. Say the actual job using an std::map takes 15 lines of code. Okay, now take the map away and replace it with your own handwritten library that, if done well, would take several hundred extra lines, at least. And those are lines of intricate algorithms that are easily broken and take a lot of effort to debug and test.

Indeed, why not just do it all in C? :rolleyes:

>This is wat we are debating isn't it?
Is that what you're talking about? I was only addressing your question about the occasional convenience of writing low level C++.

Wait a minute are you saying that you can easily use the c++ style map for integration in assembly?

But I thought you said that using objects and functions in assembly is painful. Isn't the std::map using objects and fucntions behind the scenes anyway?

>But I thought you said that using objects and functions in assembly is painful.
It's painful if you try to do it directly. However, with a good interface that meshes with assembly, you can use as much C++ as you want under the hood. Then compile to an object file and link the object file with the assembly program. At that point all you need to worry about is accessing the external name and matching the interface.

Oh ok I never knew that. I just assumed if you were using c++ in assembly you couldn't easily integrate all the benefits the STL has to offer. And so was chasing the point of why anyone would want to use c++ in assembly if that was case.

In that small exception then I can see why it may be useful to mix C++ with C style syntaxing.

However, in most cases. Like most of the ppl who come here with homework problems, they are not using peripheral devices with assembly. It is just basic programs, so I would argue they shouldn't really need an excuse to use c-style strings over std::strings.

That's all I was getting at. Is that what you understood from before.

>It is just basic programs, so I would argue they shouldn't really need
>an excuse to use c-style strings over std::strings.
Knowing the low level helps to write better at the high level. This is a simple fact, and the reason why so many students come here using C-style strings, but teachers have a tendency to take it too far.

A good teacher, in my opinion, will make writing the first programs as simple as possible and then add to the student's knowledge base by introducing conveniences or details as necessary. That way a student is capable of getting immediate results, and can gradually increase the power and flexibility of their results as they learn more. You can see your improvement, you can see the necessity of something, and you're constantly succeeding. There's no depression or boredom, and you can cover more ground.

Unfortunately, the accepted curriculum is to start with the low level and move to the high level. That means working with C-style strings and eventually getting to std::string, with a stop at that godawful MFC CString along the way. The sad part is that the class is usually over long before the students would get to the std::string part, and they walk away with an incomplete perspective, if they didn't quit half way through because they couldn't "get it" at the low level.

OOpsy gotta go now. My mom keeps telling me "You spend too much time on the darn computer"... :sad:

:cheesy:

[edit]
I understand the importance of learning the c-style string. Obviously the std::string is just a fancy wrapper for the c-style string? But I think that the c-style string should only be taught in C.

When learning c++ I don't think it does the student any justice by ignoring the benefits of the std::string and the STL. Like you said unfortunately they come to the end of the course without fully appreciating the rewards of using the STL.[/edit]
I'm out.

This article has been dead for over six months. Start a new discussion instead.