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
16 Antworten
+ 3
+ 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;
}
+ 3
cin >> word; reads a chunk to the next space before processing it as integer, string, or whatever.
+ 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;
}
+ 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] ;
+ 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;
}
+ 1
Michail Getmanskiy thanks a lot! its a bit complex for my level though 😅
0
i mean sololearn does this..? but how does one implement this in c or c++
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.
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
0
The character array must be completely defined like:
char s[100];
getline(cin, s);
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...
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.
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.
0
Jayakrishna thanks a lot! finally, i can rest!
0
Sena T wel come...