Counting characters without spaces

Hey guys I have made one program but I need to make it how to calculate whole the characters without the spaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//
#include <iostream>
#include <string>

using namespace std;

int main() {

	string name_surname=" John Smith";
	
	cout<<"length of your name and surname is:"<<" "<<name_surname.size()<<endl;
		
	return 0;
}


Now John Smith has only 9 charaters but if we calculate spaces then it will show up 11 characters.

So I need a help how to make a string function for a program that will not calculate spaces.


Thank you in advance

try this:
1
2
3
4
5
6
7
8
9
int characterCount(std::string in)
{
    int count = 0;
    for(unsigned int i = 0, i < in.size(), ++i) {
        if (!isspace(in[i]))
            ++count;
    }
    return count;
}
It shows me an error

Here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//============================================================================
// Name        : Biblioteka.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>

using namespace std;

int main() {

	string name_surname=" John Smith ";

	int characterCount(std::string in);

			    int count = 0;
			    for(unsigned int i = 0, i < in.size(), i++) {
			        if (!isspace(in[i]))
			            ++count;
			    }
			    return count;


	cout<<"Length of your name and surname is:"<<" "<<name_surname.size()<<endl;



	return 0;
}



Error 1

for(unsigned int i = 0, i < in.size(), i++) {

Error a: Expected initalizer before '<' token
Error b: Unused variable 'i'
Error c: Unused variable 'i'
Error d: Incorrect Synthax

Error 2

return count;

Error a: Expected primary-expression before 'return'
Error b: Expected ';' before return
Error c: Expected ')' before return


Can you please insert the code into the right position so I can copy whole the code please ?
Last edited on
Error 1:

A for loop has semicolons, not commas:

for(unsigned int i = 0; i < in.size(); i++) {

Probably a typo.

Second error probably comes from the first one.
Still shows the same errors, even more. Would you like to give it a try by inserting the right functions and code into the whole code and help me out to solve this problem ?
my code is a function, use it like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//
#include <iostream>
#include <string>

//<-- insert my code here

int main() {

	string name_surname=" John Smith";
	
	cout<<"length of your name and surname is:"<<" "<< characterCount(name_surname)<<endl;
		
	return 0;
}

And yes, sorry about commas.
Last edited on
Topic archived. No new replies allowed.