0

can i divide my string input by spaces???

like if i enter one input of "abc d ef" , it will act as i entered three inputs "abc" "d" and "ef" separately

27th Dec 2019, 7:58 PM
Sena Tunca
Sena Tunca - avatar
18 odpowiedzi
31st Dec 2019, 3:40 AM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 6
// C simple: char word[100]; while (1) { if (scanf("%s", &word) == EOF) break; printf("%s\n", word); } // C++ string word; while (true) { cin >> word; if (cin.fail()) break; cout << word << endl; }
31st Dec 2019, 11:28 PM
John Wells
John Wells - avatar
+ 3
cin >> word; reads a chunk to the next space before processing it as integer, string, or whatever.
27th Dec 2019, 8:29 PM
John Wells
John Wells - avatar
+ 3
for C++ convenient to divide by spaces using a stream of lines: #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string slinwords; getline(cin, slinwords ); stringstream ss( slinwords ); string sword; while( ss >> sword ){ //work with the word // }; } or int main() { string sword; while( cin >> sword ){ //work with the word // }; } for C: #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char slinwords[1000]; fgets( slinwords, sizeof(slinwords), stdin ); char* cwordb = slinwords; char* cline = strchr(slinwords, 0); int ilenw = 0; char cword[1000]; if(cline!=cwordb) do { cworde = strchr(cwordb, ' '); ilenw = (cworde?cworde:cline)-cwordb; memcpy(cword,cwordb,ilenw); cword[ilenw]=0; //work with the word // if(cworde)cwordb = cworde + 1; } while ( cworde != NULL ); return 0; }
31st Dec 2019, 2:29 AM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 1
What problem, you are getting? Can you link your code? Read the entire string, then using for loop by if(s[i+1] == ' space') Change s[i] ;
27th Dec 2019, 9:04 PM
Jayakrishna 🇮🇳
+ 1
Sena T #include <iostream> #include <cstring> using namespace std; int main() { string w; int i; getline(cin,w); char c= w[0]; for(i = 0; i < w.length()-1;i++){ w[i]=w[i+1]; if(w[i+1]==' '){ w[i]=c; c=w[i+2];i++; } } cout<<w; return 0; }
28th Dec 2019, 6:28 AM
Jayakrishna 🇮🇳
+ 1
Michail Getmanskiy thanks a lot! its a bit complex for my level though 😅
31st Dec 2019, 11:16 PM
Sena Tunca
Sena Tunca - avatar
0
i mean sololearn does this..? but how does one implement this in c or c++
27th Dec 2019, 8:07 PM
Sena Tunca
Sena Tunca - avatar
0
It's depends on what methods you used. scanf read a word by word not entire line. If you want read entire line, gets and fgets methods are there. Don't use gets method, use fgets method. Like fgets(string, 20,stdin); Where, string is a char array, 20 is no. Of chars to be read, ex:, stdin is input console. In c++, getline(cin, s); method is available for alternative to cin.
27th Dec 2019, 8:21 PM
Jayakrishna 🇮🇳
0
Jayakrishna im using a char array s[] and i tried using getline(cin, s) to get input from the user but it doesnt work... im not familiar with strings nor library functions
27th Dec 2019, 8:37 PM
Sena Tunca
Sena Tunca - avatar
0
The character array must be completely defined like: char s[100]; getline(cin, s);
27th Dec 2019, 8:40 PM
John Wells
John Wells - avatar
0
Sena T You can take input into string directly in c++ instead of Char array. If you want to char by char input use like cin>>s[i] ; by loops...
27th Dec 2019, 8:55 PM
Jayakrishna 🇮🇳
0
i want to change the last letter of every word in a sentence that user inputs. i do not know how many words that sentence includes.
27th Dec 2019, 8:56 PM
Sena Tunca
Sena Tunca - avatar
0
String s; getline(cin, s); Now s.length() gives you string length. in loop for i=0 ; s.length()-1;i++ if(s[i+1]== 'SPACE') s[i] ='change' ; cout<<s; Change your code according to this. Sena T try it first by yourself.
27th Dec 2019, 9:48 PM
Jayakrishna 🇮🇳
0
Jayakrishna thanks a lot! finally, i can rest!
30th Dec 2019, 8:39 PM
Sena Tunca
Sena Tunca - avatar
0
Sena T wel come...
30th Dec 2019, 8:43 PM
Jayakrishna 🇮🇳