【问题标题】:Lower- to uppercase, printing words of one line under each other从小写到大写,在一行下打印一个单词
【发布时间】:2013-12-07 21:37:23
【问题描述】:

我必须制作一个将小写字母转换为大写字母的小 c 程序。我已经设法做到了,但是,它的输出还没有完成。 以下是我目前所做的:

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

int main(void){

char s[100];
int i;

gets(s);

for(i = 0; i < strlen(s); ++i)
{
    s[i] = toupper(s[i]);
}

printf (s);

}

如果您插入“this is a_test”,它的输出将是“THIS IS A_TEST”。但是,它应该是:

'这个

A_TEST'

我该怎么做? 提前致谢。

【问题讨论】:

  • 所以你需要用新行替换空格?
  • 在遍历字符串时,检查字符是否为空格。如果是,用换行符替换它?如果每个空格需要多个换行符,则必须扩展字符串(char 数组)。

标签: c uppercase lowercase


【解决方案1】:
for(i = 0; i < strlen(s); ++i) {
    if(s[i] = ' ')
        s[i] = '\n');
    else
        s[i] = toupper(s[i]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多