This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
How do I tokenize a string in C++?
(37 answers)
Closed 9 years ago.
For example I have this string: hello:there:world
How do I split these three words to string array,
I mean the final result will be this:
arr[0] = "hello";
arr[1] = "there";
arr[2] = "world";
Thanks in advance.
Related
This question already has answers here:
C++ character concatenation with std::string behavior. Please explain this
(3 answers)
Closed 1 year ago.
cout<<"#" + 'a'<<endl;
string s = "#";
s += 'a';
cout<<s<<endl;
I am not able to figure out how the typecasting is working in the case "#" + 'a'
In cpp string works like an array of characters, so when you assign s = '#' it compiles like this:
s[0] = '#'
and in the second line it actually compiles like this:
s[1] = 'a'
finally, s is:
#a
This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.
This question already has answers here:
R: convert list of numbers from character to numeric
(3 answers)
Closed 7 years ago.
Suppose I have the character string
x <- " 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136"
and I want to extract the floats to end up with this vector as output:
c(1.1325, -0.9022, -0.1832, -0.5479, 0.1236, -0.6556, -1.0599, -0.8881, -0.2136)
What I managed to achieve is:
na.omit(as.numeric(strsplit(samp, split = " ")[[1]]))
My question: Is there a more efficient way?
We can use scan
scan(text=x, what=numeric(), quiet=TRUE)
#[1] 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136
This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 8 years ago.
How to break down a sentence of string type into words and store it in a vector of string type in c++?
Example
String str="my name";
Into
Vector word={" my","name"}
You can write a simple loop:
std::vector<std::string> words;
std::istringstream is("my name");
std::string word;
while (is >> word) {
// ...
words.push_back(word);
// ...
}
which in my opinion is good idea because you'll most likely need to do other things with those words apart the simple extraction of them. The body of the loop can be easily extended.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What happens to memory after ‘\0’ in a C string?
Is it possible to make a pointer's, say, third element NUL ('\0'), thereafter "erasing" the pointers remaining elements?
Assuming you're talking about C-style strings then yes, kind of:
char s[] = "abcdefgh"; // s = "abcdefgh"
// (actually contains 9 characters: "abcdefgh\0")
s[3] = '\0'; // s = "abc"
// (still contains 9 characters, but now: "abc\0efgh\0")
Note that the characters beyond s[3] haven't magically disappeared at this point - it's just that displaying the string, or passing it to any function that takes a C-style string, results in the string only appearing to contain three characters. You can still do e.g.
s[3] = 'x'; // s = "abcxefgh"
// (still contains 9 characters, but now: "abcxefgh\0")