0

有人会用我编写的以下代码引导我朝着正确的方向前进。基本上我试图让字符串中的所有其他字符都以大写字母打印,同时不考虑空格或其他非字母字符。

例如: string input = "thanks for the add" 应打印为 "ThAnKs ForR tHe AddD"

int main (void) 
{
    char* input = GetString();
    if (input == NULL)
        return 1;
    for (int i = 0, x = strlen(input); i < x; i+=2)
        input [i] = toupper(input[i]);
    printf("%s\n", input);
    return 0;
}

注意:我是计算机科学的新手,目前正在通过 edx.org 学习 CS50x

4

2 回答 2

1

只是isalpha用来忽略其他类型的字符:

#include <string.h>
#include <stdio.h>
#include <ctype.h>
int main (void) 
{
    char input[] = "thanks for the add";
    int  alpha_count = 0;
    for (int i = 0, x = strlen(input); i < x; i++) {
      if (isalpha(input[i])) {
        if (alpha_count++ % 2 == 0 ) 
          input [i] = toupper(input[i]);
      }   
    }   
    printf("%s\n", input);
    return 0;
}
于 2013-03-19T02:33:44.850 回答
-1
#include <iostream>
using namespace std;

int main()
{
    char st[] = "thanks for the add";
    for(int i=0; i<strlen(st); i++)
        st[i++]=toupper(st[i]);
    cout<<st<<endl;
}
于 2017-12-13T22:26:54.017 回答