#include <iostream>
#include <string>
std::string space_x3( const std::string& str )
{
std::string result_string ;
for( char c : str ) // for each char c in the string str
{
// if it is a space, append three spaces to result_string
if( c == ' ' ) result_string += " " ;
else result_string += c ; // otherwise append the character as it is
}
return result_string ;
}int main()
{
std::string str ;
std::cout << "enter a string: " ;
std::getline( std::cin, str ) ;
const std::string spaced_out_str = space_x3(str) ;
std::cout << spaced_out_str << '\n' ;
}
c[i] = c[i] * 3;
C++ doesn't work that way. What this code actually does is take the numeric representation of c[i] (aka the ASCII value) and multiply it by 3, then it replaces the the i'th character (c[i]) with that new value.
You can also avoid the array or string altogether:
#include <iostream>
#include <cstring>
int main()
{
// a test C string
char src_str[] = "This is a test string";
std::cout << "Splitting \"" << src_str << "\" into tokens:\n\n";
// get the first C string token, if any
char* pch = std::strtok(src_str, " ");
// create an empty destination C string
char dest_str[100] { };
// loop while there are valid C string tokens
while (pch != nullptr)
{
// append the valid token to the destination C string
std::strcat(dest_str, pch);
// append 3 spaces to the C string
std::strcat(dest_str, " ");
// get the next token
pch = std::strtok(NULL, " ");
}
std::cout << dest_str << '\n';
}
The issue with strtok() is that it modifies the specified string. The dest_str is right, but src_str is now not as before. src_str cannot be const even when a new string is being constructed.
constructing a new string is the most efficient way, I believe.
then it is simply
1 2 3 4 5 6 7 8 9 10 11 12 13
char newstr[1000] = {0}; //important: newstr needs to be zero filled so terminal zero in place
char oldstr[] = "this is a test and stuff";
for(int i =0, j=0; i < strlen(oldstr); i++)
{
if(oldstr[i] == ' ') //I can't think of a clean way to avoid this.
{
newstr[j++] = ' ';
newstr[j++] = ' ';
}
newstr[j++] = oldstr[i]; //notice this gets the third space by copying the original.
}
Yes, std::strtok is destructive to the C string being tokenized. So? Where does the OP say they need to retain the original C string as being UN-modified? In their posted code the original C string is being modified.
src_str cannot be const even when a new string is being constructed.
So? Where does the OP state they have to use a constant C string?
If retaining an un-modified original C string is required they can either use other C string functions that don't modify the original, tokenize a copy of the original or any one of several different ways of modifying the original C string while creating a new C string that holds the modifications.
seeplus, the output you show is NOT from the code I posted. It looks as if you added an additional output statement. Only two outputs are in my code, before and after modifying the C string. Not when while doing the tokenization.