2
#include <iostream> #include <string> #include <cctype> using namespace std; int main() { string passCode; passCode = "1 "; int i; for(i =0; i < passCode.length();i++){ if(isspace(passCode.at(i)) == true){ passCode.replace(i,1,"_"); } } cout << passCode << endl; return 0; } 

Code above, my directions are to [Replace any space ' ' by '_' in 2-character string passCode. If no space exists, the program should not print anything.]

with my code currently the way it is, it outputs "1 ". When i run it with the condition checking for false instead of true, it prints "_ ". I am not getting why its doing this, anyone see the problem that i don't? I am not allowed to use the algorithm. header. I am also only allowed to work within main, no functions or imported headers/classes.

5 Answers 5

4

For single characters, it may be easier to use the std::replace algorithm:

std::replace(passCode.begin(), passCode.end(), ' ', '_'); 

If you can't use the algorithm header you can roll out your own replace function. It can be done with a simple loop:

template<typename Iterator, typename T> void replace(Iterator begin, Iterator end, const T& old_val, const T& new_val) { for (; begin != end; ++begin) if (*begin == old_val) *begin = new_val; } 
Sign up to request clarification or add additional context in comments.

8 Comments

I think replace_if + isspace might be fitted better here. It's also used in the OP's code.
@cad Instructions say "Replace any space ' ' by '_' ", so using isspace would do more than that.
This doesn't work, it says replace isn't a member of std::
@Flower It works if you read the documentation in the link and include the required header.
@Flower Please stop wasting people's time. If you have ridiculous restrictions, they should be well explained in your question. std::string is a typedef for a template, so you're already breaking your own rules.
|
1

with my code currently the way it is, it outputs "1 ". When i run it with the condition checking for false instead of true, it prints "_ "

isspace returns a non-zero value when it is passed a space. This need not be exactly 1. On the other hand, the boolean true is usually set to 1.

When we compare the return value of isspace with true, what happens when they are not exactly equal? Specifically what if true is 1 and isspace returns just some non-zero value?

I think that is what is happening here. The if condition is failing because these two are different values. So space is not being replaced by '_'.

1 Comment

Yea, in my program it was evaluating to some positive value, thanks for the help.
1

Your problem is your use of isspace. If you read the documentation for isspace it says:

Return Value
A value different from zero (i.e., true) if indeed c is a white-space character. Zero (i.e., false) otherwise.

However, you are only checking if it returns true or false. Your compiler should be warning you about the mismatch, since isspace returns an int and you're checking for a bool.

Changing to the following code should work for you:

if(isspace(passCode.at(i)) != 0) { passCode.replace(i,1,"_"); } 

My answer is based more specifically around your question and your comment saying you can't use any headers besides what you've included. A far better solution has been answered by juanchopanza and you should en devour to use the standard library whenever you can, rather than writing your own code.

Comments

1

You could also do this with a while loop controled with std::string::find and replacing the spaces with std::string::replace.

std::string test = "this is a test string with spaces "; std::size_t pos = 0; while ((pos = test.find(' ', pos)) != std::string::npos) { test.replace(pos, 1, "_"); pos++; } std::cout << test; 

Live Example

Comments

0

As described here, isspace doesn't return bool. It, instead, returns int where non-zero value represents true, and zero value represents false. You should write the check like this:

if (isspace(passCode.at(i)) != 0) 

3 Comments

I see, this is how it was explained to me: true if whitespace isspace(' ') // true isspace('\n') // true isspace('x') // false no wonder it was not working. thanks alot
@Flower In the context of how if/while, and other conditional statements work in C/C++ is that values that are non-zero - are assumed to be TRUE, and values that are equal to zero - are FALSE. So, technically, you could even write your if like this: if (isspace(passCode.at(i))).
Note that this will do more than "Replace any space ' '".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.