Array and String
-
char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks
-
char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks
-
char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks
Apparently, you are printing the result with the following:
std::cout << beta << std::endl
. If you print it withstd::cout << beta.c_str() << std::endl
, you get the desired print out. However, if you actually want to remove the characters following 'a' you can do this:#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string beta = "abcdefg";
beta.erase(beta.begin() + 1, beta.end());
std::cout << "Result #1: " << beta << std::endl;
std::cout << "Result #2: " << beta.c_str() << std::endl;
return 0;
}Also, please post C++ questions in the C++ forum and not the C++/CLI forum.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
#include void main() { char string1[10]="abcdef"; char string2[10]="abcdef"; string1[1]='\0'; string2[1]='\0'; printf("%s\n%s",string1,string2); } result: a a ---- i don't think it is wrong:rolleyes:
ubri
Unfortunately, he is trying to modify a
std::string
and not a char array string."We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Apparently, you are printing the result with the following:
std::cout << beta << std::endl
. If you print it withstd::cout << beta.c_str() << std::endl
, you get the desired print out. However, if you actually want to remove the characters following 'a' you can do this:#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string beta = "abcdefg";
beta.erase(beta.begin() + 1, beta.end());
std::cout << "Result #1: " << beta << std::endl;
std::cout << "Result #2: " << beta.c_str() << std::endl;
return 0;
}Also, please post C++ questions in the C++ forum and not the C++/CLI forum.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks
You are little bit consfused with char[] and std :: string Please Clear about that.
Best Regards, Chetan Patel
-
#include void main() { char string1[10]="abcdef"; char string2[10]="abcdef"; string1[1]='\0'; string2[1]='\0'; printf("%s\n%s",string1,string2); } result: a a ---- i don't think it is wrong:rolleyes:
ubri
-
You are little bit consfused with char[] and std :: string Please Clear about that.
Best Regards, Chetan Patel
Hi. I got my answer but you're right. I'm reading the c++ Primer + and there are different chapters to cover String. I did not read the chapter which explains in details. It sucks to be a beginner :-):)
{ in a land with no bird, no spring. My first journey was a return 0; }