试图带走某人';s全名并用C返回其首字母缩写

试图带走某人';s全名并用C返回其首字母缩写,c,C,我是C语言的新手,我正在尝试编写一个程序,该程序采用输入的名称,如john smith,并返回大写字母缩写JS。我尝试过使用for循环和while循环,但我的代码似乎没有增加,每当我运行它时,它返回的都是第一个初始值。我在网上搜索过这个问题,但没有一个解决方案适合我。我做错了什么?提前谢谢 代码如下: #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h&

我是C语言的新手,我正在尝试编写一个程序,该程序采用输入的名称,如john smith,并返回大写字母缩写JS。我尝试过使用for循环和while循环,但我的代码似乎没有增加,每当我运行它时,它返回的都是第一个初始值。我在网上搜索过这个问题,但没有一个解决方案适合我。我做错了什么?提前谢谢

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main(void) {
  // initialize variables
  char name[61];
  int i = 0;

  // ask for user input
  printf("Please enter your name: ");
  scanf("%s", name);

  // print first initial
  printf("%c", toupper(name[0]));

  // print the next ones
  while (name[i] != '\0') {
    if (name[i] == ' ') {
      i++;
      printf("%c", toupper(name[i+1]));
    }
    i++; // does not increment
  }
  return 0;
}
#包括
#包括
#包括
#包括
内部主(空){
//初始化变量
字符名[61];
int i=0;
//请求用户输入
printf(“请输入您的姓名:”);
scanf(“%s”,名称);
//打印首字母
printf(“%c”,toupper(名称[0]);
//打印下一张
while(名称[i]!='\0'){
如果(名称[i]=''){
i++;
printf(“%c”,toupper(名称[i+1]);
}
i++;//不递增
}
返回0;
}
scanf(“%s”,name)
只读取名字。您需要类似于
scanf(“%s%s”,名字,姓氏)
scanf()
读取输入,直到遇到空格。所以写全名在名字和姓氏之间有一个空格。这将停止
scanf()
,它将只读取名字

要用空格读取两个输入,最好使用
fgets()
fgets()
读取字符串,直到遇到换行符
\n

fgets(name, 61, stdin);

别忘了替换
i++if
中使用
if(isalpha(名字[i+1]))
@jedimaster7,不客气。如果可能的话,请送给我一把光剑@绝地大师7,顺便说一句,你不知道黑暗面的力量如果我知道你不会把它用于黑暗面,我会送给你一个…;)@绝地大师7,你知道我最终会变好的,让我从黑暗面中得到一点乐趣。谢谢:),我不知道scanf()会在第一次之后停止word@jedimaster7
%s
scanf
中扫描,直到出现空白字符。