-1

我正在编写一个代码,该代码采用包含某人姓名的字符串并打印该姓名的首字母大写,每当我运行我的代码时,我都会将首字母打印两次,我不知道如何解决这个问题和得到想要的输出。

这是我的代码:

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

int main(void)
{
    string name = GetString();
    char* pointer;
    pointer = strtok(name, " ");
    while (pointer != NULL)
    {
        printf("%c", putchar(toupper(pointer[0])));
        pointer = strtok (NULL, " ");
    }
    printf("\n");
}

当我运行代码时,例如:ahmed salah eldin

输出:

AASSEE

我只需要 :

ASE
4

1 回答 1

0

你正在使用printf()putchar()你只需要其中之一。当您调用putchar()它时,它会返回输出字符,然后将其传递给printf()并再次输出。

将其更改为

fputc(toupper(pointer[0]), stdout);
于 2016-02-07T20:50:34.230 回答