Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用getchar替换get的用法_C_Getchar_Gets - Fatal编程技术网

用getchar替换get的用法

用getchar替换get的用法,c,getchar,gets,C,Getchar,Gets,我目前有一个家庭作业,我使用了get。 教授说我应该改用getchar 有什么区别 如何将代码更改为使用getchar?我似乎做得不对 代码: #包括 #包括 #包括 #定义存储255 int main(){ INTC; 字符s[存储]; 对于(;;){ (void)printf(“n=%d,s=[%s]\n”,c=getword,s); 如果(c==-1)中断; } } int getword(字符*w){ char-str[255]; int i=0; int charCount=0; pr

我目前有一个家庭作业,我使用了
get
。 教授说我应该改用
getchar

有什么区别

如何将代码更改为使用
getchar
?我似乎做得不对

代码:

#包括
#包括
#包括
#定义存储255
int main(){
INTC;
字符s[存储];
对于(;;){
(void)printf(“n=%d,s=[%s]\n”,c=getword,s);
如果(c==-1)中断;
}
}
int getword(字符*w){
char-str[255];
int i=0;
int charCount=0;
printf(“输入您的句子:\n”);//用户输入
获取(str);
对于(i=0;str[i]!='\0'&&str[i]!=EOF;i++){
如果(str[i]!=''){
charCount++;
}否则{
str[i]='\0';//终止str
i=-1;//idk这到底在做什么?
break;//中断for循环
}
}
printf(“您的字符串:“%s”包含%d个字母\n”、str、charCount);//输出
strcpy(w,str);
//返回字符数;
return strlen(w);//不确定我应该返回什么……它们都有效
}
get()
应该从输入中获取一个字符串,并将其存储到提供的参数中。但是,由于缺乏对输入长度的初步验证,它很容易发生缓冲区溢出

更好的选择是

然而,说到part的用法,它一次读取一个
char
。因此,基本上,您必须使用循环一个接一个地读取标准输入,直到到达一条换行线(或EOF),这标志着预期输入的结束

在读取字符时(可选验证),您可以继续将其存储在
str
中,这样,当输入循环结束时,您就可以在
str
中准备好输入字符串

别忘了使用null终止
str
,以防万一。

gets()
应该从输入中获取一个字符串并将其存储到提供的参数中。但是,由于缺乏对输入长度的初步验证,它很容易发生缓冲区溢出

更好的选择是

然而,说到part的用法,它一次读取一个
char
。因此,基本上,您必须使用循环一个接一个地读取标准输入,直到到达一条换行线(或EOF),这标志着预期输入的结束

在读取字符时(可选验证),您可以继续将其存储在
str
中,这样,当输入循环结束时,您就可以在
str
中准备好输入字符串


别忘了用null终止
str
,以防万一。

请编辑您的帖子并修复缩进和代码格式。主要区别在于C语言中没有名为
get
的函数。它在5年前被删除,并在17年前被标记为过时。这取决于您正在尝试做什么,但是
getchar()
通常不是
gets()
的首选替代品
fgets()
是首选-因为它可以用来读取一行,但也可以以安全的方式使用,不像
gets()
。请编辑post和fix缩进以及代码格式。主要区别在于C语言中没有名为
gets
的函数。它在5年前被删除,并在17年前被标记为过时。这取决于您正在尝试做什么,但是
getchar()
通常不是
gets()
的首选替代品
fgets()
是首选,因为它可以用来读取一行,但也可以以安全的方式使用,不像
gets()
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define STORAGE 255

int main() {
  int c;
  char s[STORAGE];

  for(;;) {
    (void) printf("n=%d, s=[%s]\n", c = getword(s), s);
    if (c == -1) break;
  }
}

int getword(char *w) {
  char str[255];
  int i = 0;
  int charCount = 0;

  printf("enter your sentence:\n");   //user input
  gets(str);

  for(i = 0; str[i] != '\0' && str[i] !=EOF; i++){
    if(str[i] != ' '){
      charCount++;
    } else {
      str[i] = '\0'; //Terminate str
      i = -1; //idk what this is even doing?
      break;      //Break out of the for-loop
    }
  }

  printf("your string: '%s' contains %d of letters\n", str, charCount); //output
  strcpy(w, str);

  //    return charCount;
  return strlen(w); //not sure what i should be returning.... they both work
}