C 函数获取单词并将它们放入数组中
我需要编写一个C函数,从用户那里获取他想要输入的单词数,然后该函数必须扫描来自用户的单词,但不扫描数组中的单词 例如: 节目:C 函数获取单词并将它们放入数组中,c,arrays,string,function,C,Arrays,String,Function,我需要编写一个C函数,从用户那里获取他想要输入的单词数,然后该函数必须扫描来自用户的单词,但不扫描数组中的单词 例如: 节目: number of words: 用户: 3 hi my name (每个单词之间都有enter)然后函数必须将这些单词放入 字符串数组(数组的大小必须由malloc定义,字符串的最大大小为100(可以更小)) intmain() { int n; printf(“请输入字数:\n”); 如果(scanf(“%d”,&n)!=1) 返回0; 字符*名称; 名称=ma
number of words:
用户:
3
hi
my
name
(每个单词之间都有enter)然后函数必须将这些单词放入
字符串数组(数组的大小必须由malloc定义,字符串的最大大小为100(可以更小))
intmain()
{
int n;
printf(“请输入字数:\n”);
如果(scanf(“%d”,&n)!=1)
返回0;
字符*名称;
名称=malloc((sizeof(char)*100*n));
INTC;
int i;
int m;
对于(i=0;i
当您需要存储字符串数组时,您需要一个char*
或char**
数组来指向每个字符串(char数组)
然后在循环中使用fgets
将输入读取为一行。此外,还需要为每个新的char
数组分配内存
fflush(stdin);
for (i = 0; i < n; i++) {
name[i] = malloc(100); // allocating memory for string.
fgets (name[i], 100, stdin); // 100 is the max len
}
您需要设置指向指针的指针
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char **s;
int n;
char buffer[64];
fgets(buffer,64,stdin);
n=strtol(buffer,NULL,10);// I avoid using scanf
s=(char **)malloc(sizeof(char*)*n);// you need to declare a pointer to pointer
/*
'PtP s' would look like this:
s[0]=a char pointer so this will point to an individual string
s[1]=a char pointer so this will point to an individual string
s[2]=a char pointer so this will point to an individual string
....
so you need to allocate memory for each pointer within s.
*/
int i;
for(i=0;i<n;i++){
s[i]=(char*)malloc(sizeof(char)*100);// length of each string is 100 in this case
}
for(i=0;i<n;i++){
fgets(s[i],100,stdin);
if(strlen(s[i])>=1){// to avoid undefined behavior in case of null byte input
if(s[i][strlen(s[i])-1]=='\n'){ // fgets also puts that newline character if the string is smaller than from max length,
s[i][strlen(s[i])-1]='\0'; // just removing that newline feed from each string
}
else{
while((getchar())!='\n'); //if the string in the command line was more than 100 chars you need to remove the remaining chars for next fgets
}
}
}
for(i=0;i<n;i++){
printf("\n%s",s[i]);
}
for(i=0;i<n;i++){
free(s[i]); //avoiding leaks
}
free(s);
}
#包括
#包括
#包括
int main(){
字符**s;
int n;
字符缓冲区[64];
fgets(缓冲区,64,标准输入);
n=strtol(buffer,NULL,10);//我避免使用scanf
s=(char**)malloc(sizeof(char*)*n);//您需要声明指向指针的指针
/*
“PtP s”将如下所示:
s[0]=一个字符指针,因此它将指向单个字符串
s[1]=一个字符指针,因此它将指向一个单独的字符串
s[2]=一个字符指针,因此它将指向一个单独的字符串
....
因此,您需要为s中的每个指针分配内存。
*/
int i;
对于(i=0;听起来像是作业。到目前为止你试过什么?@x29a我把我写的代码放在stackoverflow.com上,我认为我写的代码是错误的,关于这种作业有很多问题。建议使用stackoverflow.com搜索引擎公开这些问题并检查相应的答案维尔斯
fflush(stdin);
for (i = 0; i < n; i++) {
name[i] = malloc(100); // allocating memory for string.
fgets (name[i], 100, stdin); // 100 is the max len
}
for (i = 0; i < n; i++) {
// printf("%s", name[i]);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char **s;
int n;
char buffer[64];
fgets(buffer,64,stdin);
n=strtol(buffer,NULL,10);// I avoid using scanf
s=(char **)malloc(sizeof(char*)*n);// you need to declare a pointer to pointer
/*
'PtP s' would look like this:
s[0]=a char pointer so this will point to an individual string
s[1]=a char pointer so this will point to an individual string
s[2]=a char pointer so this will point to an individual string
....
so you need to allocate memory for each pointer within s.
*/
int i;
for(i=0;i<n;i++){
s[i]=(char*)malloc(sizeof(char)*100);// length of each string is 100 in this case
}
for(i=0;i<n;i++){
fgets(s[i],100,stdin);
if(strlen(s[i])>=1){// to avoid undefined behavior in case of null byte input
if(s[i][strlen(s[i])-1]=='\n'){ // fgets also puts that newline character if the string is smaller than from max length,
s[i][strlen(s[i])-1]='\0'; // just removing that newline feed from each string
}
else{
while((getchar())!='\n'); //if the string in the command line was more than 100 chars you need to remove the remaining chars for next fgets
}
}
}
for(i=0;i<n;i++){
printf("\n%s",s[i]);
}
for(i=0;i<n;i++){
free(s[i]); //avoiding leaks
}
free(s);
}