gpt4 book ai didi

c 中字符串首字母大写 - 在 110 个字符以下编写 myprintf 函数

转载 作者:太空宇宙 更新时间:2023-11-04 00:43:30 24 4
gpt4 key购买 nike

我需要制作一个 my_printf 函数,它接受一个字符串,只将字符串的第一个字母大写(即使之前有空格),然后做一个\n,全部在 110 个字符以内(不包括空格/制表符)。

我只能修改注释“TO BE DONE START”和“TO BE DONE END”之间的函数。

这是我到目前为止编写的代码:我遇到的唯一问题是在输出中它没有将\t 之后的“looks OK :)”的字母“l”大写,我有不知道如何在不超过此代码中 110 个字符的最大限制的情况下实现不在字符串 q[0] 位置的字符的大写;我知道它需要一个循环,但我似乎总是超出限制。

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

void my_printf(char*p){
char s[strlen(p)+1], *q=s;
strcpy(s,p);

/* TO BE DONE START */
q[0]=toupper(q[0]);
putchar(q[0]);
for(*q=1;*q!='\0';++q) {
putchar(*q);
}
putchar('\n');

/* TO BE DONE END */
}

int main(){
my_printf("hello world!");
my_printf("How are you?");
my_printf("i\'m OK, and you?");
my_printf("1, 2, 3, testing ...");
my_printf("\t looks OK :-)");
my_printf("bye bye!");
return 0;
}

我需要帮助使此代码尽可能短,这是所需的输出:

 Hello world!
How are you?
I'm OK, and you?
1, 2, 3, testing …
Looks OK :-)
Bye bye!

虽然我的是:

 Hello world!
How are you?
I'm OK, and you?
1, 2, 3, testing …
looks OK :-)
Bye bye!

最佳答案

您可以使用 isspace 函数 - 类似于:

/* TO BE DONE START */
while (isspace(*q)) putchar(*q++);
for(*q = toupper(*q); *q; ) putchar(*q++);
putchar('\n');
/* TO BE DONE END */

关于c 中字符串首字母大写 - 在 110 个字符以下编写 myprintf 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55533187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com