+ 1
String question
This question appeared on discord and I want to help, but I myself don't know the solution. The user wanted a string input, and perform certain actions for certain words within the string. Like, if the input is "Hello World!" it'll do something for the word "Hello" However, he needs to handle each occurrence of specified words, so assume the words can appear more than once and assume there can be multiple keywords in the string. Please respond with C++
3 odpowiedzi
+ 1
If i got it correctly, you want to split string into substrings separated with space
getline (cin, s);
for(int i=0;i<s.size();i++){
If(s[i]==' '){
k++;
}
Else{
Arr[k]+=s[i];
}
}
Arr-array of substrings
S-input
K-current position of array
This is the code to split string into words(if you don't input several spaces)
After that, you can do whatever you want with string in array
+ 1
Дмитрий Мазыло Can you send me a working version? I want to use a vector since the number of words will be unknown, but I can't seem to get it to work. It seems to work with arrays just fine tho.
Also, thanks for the answer.
0
getline (cin, s);
for(int i=0;i<s.size();i++){
If(s[i]==' '){
Arr.pb(cur);
cur="";
}
Else{
cur+=s[i];
}
}