【问题标题】:Splitting strings and printing the capitalized first characers in C在 C 中拆分字符串并打印大写的第一个字符
【发布时间】:2016-02-07 20:47:49
【问题描述】:

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

这是我的代码:

#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

【问题讨论】:

  • 你为什么使用 putchar printf?它们都输出一个字符。

标签: c cs50 toupper putchar


【解决方案1】:

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

改成

fputc(toupper(pointer[0]), stdout);

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 2023-02-23
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2021-10-22
    相关资源
    最近更新 更多