简体   繁体   English

使用C进行小写到大写转换

[英]Lower to Upper Case Conversion with C

Problem Statement 问题陈述

I'm facing difficulty in solving a programming contest problem, which reads as follows: 我在解决编程竞赛问题时遇到困难,其内容如下:

You are given T name(s) in english letters. 您会获得英文字母的T名称。 Each name will include some of the uppercase letters from A to Z, some of the lowercase letters from a to z and some spaces. 每个名称将包含从A到Z的一些大写字母,从a到z的一些小写字母以及一些空格。 You have to transform the name(s) from lowercase to uppercase. 您必须将名称从小写转换为大写。 Letters that are originally uppercase will remain the same and the spaces will also remain in their places. 最初为大写的字母将保持不变,并且空格也将保留在其位置。


Sample Input-Output 样本输入输出

If I type this in... 如果我输入...

5
Hasnain Heickal    Jami
Mir Wasi Ahmed
Tarif Ezaz
     Mahmud Ridwan
Md    Mahbubul Hasan

the computer should output this... 电脑应该输出这个...

Case 1: HASNAIN HEICKAL    JAMI
Case 2: MIR WASI AHMED
Case 3: TARIF EZAZ
Case 4:      MAHMUD RIDWAN
Case 5: MD    MAHBUBUL HASAN
  • Note that exactly one space is required between the semi-colon and the initial letter of the name. 请注意,分号和名称的首字母之间仅需要一个空格。

My Coding 我的编码

This is what I've coded in C : 这就是我用C编写的代码:

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

int main(void)
{
    int T, i;
    char string [100];
    scanf("%d", &T);

    for (i=0; i<T; i++) 
    {
        gets(string);
        printf("Case %d: ", i);

        while (string[i])
        {
            putchar (toupper(string[i]));
            i++;
        }          

        printf("\n");
     }  

    getch();
    return 0;
}

Now, this code fails to produce the desired output. 现在,此代码无法产生所需的输出。 Where am I doing it wrong? 我在哪里做错了? Is there any matter with my syntax? 我的语法有什么问题吗? Can somebody guide me? 有人可以指导我吗? Please bear in mind that I'm a middle-schooler and just a beginner in C. 请记住,我是一名中学生,只是C语言的初学者。

You need to cycle over each letter of the string one-by-one. 您需要一个一个地循环遍历字符串的每个字母。

In this code below, I have done that with variable K , which goes from 0 to the length of the string. 在下面的代码中,我使用变量K进行了此操作,变量K从0到字符串的长度。

Variable I keeps track of the number of strings. 变量I跟踪字符串的数量。

int main(void)
{
  int T, i, k;
  char string [100];

  scanf("%d", &T);

  for ( i = 0; i < T; ++i)
  {
     gets (string);

     for(k=0; k<strlen(string); ++k)
     {
         putchar (toupper(string[k]));
     }
  }  

  getch();
  return 0;
}

In response your question: IDEOne Link 回答您的问题: IDEOne链接

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

int main(void)
{
  int T, i,k;
  char string [100];

  scanf("%d ", &T);

  for ( i = 0; i < T; ++i)
  {
     gets (string);
     printf("[%d] : %s\n", i, string);

     for(k=0; k<strlen(string); ++k)
     {
         putchar (toupper(string[k]));
     }
     putchar('\n');
  }  

  return 0;
}

Please go through the code and implement the test cases scenarios as per your requirement. 请仔细阅读代码并根据您的要求实施测试用例场景。

#include <stdio.h>
#include<string.h>
int main(){
  char string[100];
  int i;
  scanf("%s",string);
  for(i=0;i<strlen(string);i++){
       string[i]=string[i]-32;
  }
  printf("%s",string);
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM