I want to read numbers(integer type) separated by spaces using scanf() function.
I have read the following
我想要读取使用scanf()函数分隔的数字(integer类型)。我读过以下内容
C, reading multiple numbers from single input line (scanf?)
how to read scanf with spaces
It doesn't help me much. How can I read numbers with space as delimiter. For e.g. I have following numbers as input 2 5 7 4 3 8 18
now I want to store these in different variables.
Please help.
从单输入行读取多个数字(scanf?)如何用空格来读取scanf对我的帮助不大。如何以空格作为分隔符来读取数字。例如,我有以下数字作为输入2 5 7 4 3 8 18现在我想把它们存储在不同的变量中。请帮助。
8 个解决方案
#1
31
I think by default values read by scanf with space/enter. Well you can provide space between '%d' if you are printing integers. Also same for other cases.
我认为scanf使用空格/enter的默认值。如果要打印整数,可以在'%d'之间提供空格。其他情况也是如此。
scanf("%d %d %d", &var1, &var2, &var3);
Similarly if you want to read comma separated values use :
类似地,如果你想读逗号分隔的值,请使用:
scanf("%d,%d,%d", &var1, &var2, &var3);
#2
3
It should be as simple as using a list of receiving variables:
它应该像使用接收变量列表一样简单:
scanf("%i %i %i", &var1, &var2, &var3);
scanf(“%i %i %i”,&var1, &var2, &var3);
#3
3
Read as %s[^\n]
解读为% s[^ \ n]
and then read each char
of the string , and do a atoi()
if it is a char
, else ignore it.
然后读取字符串的每个字符,如果它是一个字符,则执行atoi(),否则忽略它。
#4
2
int main()
{
char string[200];
int g,a,i,G[20],A[20],met;
gets(string);
g=convert_input(G,string);
for(i=0;i<=g;i++)
printf("\n%d>>%d",i,G[i]);
return 0;
}
int convert_input(int K[],char string[200])
{
int j=0,i=0,temp=0;
while(string[i]!='\0')
{
temp=0;
while(string[i]!=' ' && string[i]!='\0')
temp=temp*10 + (string[i++]-'0') ;
if(string[i]==' ')
i++;
K[j++]=temp;
}
return j-1;
}
#5
2
scanf
uses any whitespace as a delimiter, so if you just say scanf("%d", &var)
it will skip any whitespace and then read an integer (digits up to the next non-digit) and nothing more.
scanf使用任何空格作为分隔符,所以如果你只说scanf(“%d”,&var),它将跳过任何空格,然后读取一个整数(到下一个非数字的数字),再没有别的了。
Note that whitespace is any whitespace -- spaces, tabs, newlines, or carriage returns. Any of those are whitespace and any one or more of them will serve to delimit successive integers.
注意,空格是任何空格——空格、制表符、换行符或回车符。其中任何一个都是空格,任何一个或多个空格都可以用来分隔连续的整数。
#6
0
C++ code in TurboC++ compiler:
c++ TurboC中的代码+编译器:
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void main()
{
int l=0,i=0;
char *a,*t;
clrscr();
int n;
cout<<"\nEnter a string: ";
cin>>t;
a=strdup(t); //duplicate
while(strlen(a)){
a = strtok(a, ","); //gives string upto where ',' is found
n=atoi(a);
l+=strlen(a); //we save the length
cout<<"n= "<<n<<" a= "<<a<<" "<<strlen(a)<<endl; //simple output
a=strdup(t+l+i); //updating the same pointer with rest part
i++; //counting number of ','s
}
getch();
}
#7
-1
int main(){
int A[1000];
char *tok = NULL;
char *savePtr = NULL;
char str[1000] = "";
int totalElement=0;
printf("Enter space seperated string\n");
scanf("%[^\n]s", str);
savePtr = str;
while((tok=strtok_r(savePtr, " ", &savePtr))){
A[totalElement++] = atoi(tok);
}
for(int i = 0; i < totalElement; i++)
printf("%d ", A[i]);
}
#8
-1
这里我只写了一个简单的c代码,用于添加两个数字并从用户那里获取输入。现在a和b是输入,%d%或%d%d都不重要,只要输入被要求,你要做的就是写第一个输入然后按空格键然后写第二个输入,等等。即使你只是在%d之间放个空格,也没有变化。因此,我认为最好的办法就是这样。
#1
31
I think by default values read by scanf with space/enter. Well you can provide space between '%d' if you are printing integers. Also same for other cases.
我认为scanf使用空格/enter的默认值。如果要打印整数,可以在'%d'之间提供空格。其他情况也是如此。
scanf("%d %d %d", &var1, &var2, &var3);
Similarly if you want to read comma separated values use :
类似地,如果你想读逗号分隔的值,请使用:
scanf("%d,%d,%d", &var1, &var2, &var3);
#2
3
It should be as simple as using a list of receiving variables:
它应该像使用接收变量列表一样简单:
scanf("%i %i %i", &var1, &var2, &var3);
scanf(“%i %i %i”,&var1, &var2, &var3);
#3
3
Read as %s[^\n]
解读为% s[^ \ n]
and then read each char
of the string , and do a atoi()
if it is a char
, else ignore it.
然后读取字符串的每个字符,如果它是一个字符,则执行atoi(),否则忽略它。
#4
2
int main()
{
char string[200];
int g,a,i,G[20],A[20],met;
gets(string);
g=convert_input(G,string);
for(i=0;i<=g;i++)
printf("\n%d>>%d",i,G[i]);
return 0;
}
int convert_input(int K[],char string[200])
{
int j=0,i=0,temp=0;
while(string[i]!='\0')
{
temp=0;
while(string[i]!=' ' && string[i]!='\0')
temp=temp*10 + (string[i++]-'0') ;
if(string[i]==' ')
i++;
K[j++]=temp;
}
return j-1;
}
#5
2
scanf
uses any whitespace as a delimiter, so if you just say scanf("%d", &var)
it will skip any whitespace and then read an integer (digits up to the next non-digit) and nothing more.
scanf使用任何空格作为分隔符,所以如果你只说scanf(“%d”,&var),它将跳过任何空格,然后读取一个整数(到下一个非数字的数字),再没有别的了。
Note that whitespace is any whitespace -- spaces, tabs, newlines, or carriage returns. Any of those are whitespace and any one or more of them will serve to delimit successive integers.
注意,空格是任何空格——空格、制表符、换行符或回车符。其中任何一个都是空格,任何一个或多个空格都可以用来分隔连续的整数。
#6
0
C++ code in TurboC++ compiler:
c++ TurboC中的代码+编译器:
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void main()
{
int l=0,i=0;
char *a,*t;
clrscr();
int n;
cout<<"\nEnter a string: ";
cin>>t;
a=strdup(t); //duplicate
while(strlen(a)){
a = strtok(a, ","); //gives string upto where ',' is found
n=atoi(a);
l+=strlen(a); //we save the length
cout<<"n= "<<n<<" a= "<<a<<" "<<strlen(a)<<endl; //simple output
a=strdup(t+l+i); //updating the same pointer with rest part
i++; //counting number of ','s
}
getch();
}
#7
-1
int main(){
int A[1000];
char *tok = NULL;
char *savePtr = NULL;
char str[1000] = "";
int totalElement=0;
printf("Enter space seperated string\n");
scanf("%[^\n]s", str);
savePtr = str;
while((tok=strtok_r(savePtr, " ", &savePtr))){
A[totalElement++] = atoi(tok);
}
for(int i = 0; i < totalElement; i++)
printf("%d ", A[i]);
}
#8
-1
这里我只写了一个简单的c代码,用于添加两个数字并从用户那里获取输入。现在a和b是输入,%d%或%d%d都不重要,只要输入被要求,你要做的就是写第一个输入然后按空格键然后写第二个输入,等等。即使你只是在%d之间放个空格,也没有变化。因此,我认为最好的办法就是这样。