【问题标题】:Converting lower/upper case letters without ctype.h在没有 ctype.h 的情况下转换小写/大写字母
【发布时间】:2012-11-13 23:27:00
【问题描述】:

我刚刚看到这在技术上是可行的,唯一我无法解决的错误是每次测试时打印的最后一个 ASCII 字符,我也在不使用 name 变量,我的意思是只要对 ASCII 中的任何小写字母减去 32 就应该给我他们的大写字母,而且确实如此,但我很好奇为什么我会得到一个额外的字符,我在屏幕上看到的显然是 Û

#include <stdio.h>
 main()
{
char name[22];
int i;

fputs("Type your name ",stdout);
fgets(name,22,stdin);


for (i = 0; name[i] != '\0'; i = i + 1)
printf("%c",(name[i])-32);  /*This will convert lower case to upper */
                            /* using as reference the ASCII table*/   
fflush(stdin);
getchar();
} 

【问题讨论】:

    标签: ascii ctype tolower toupper


    【解决方案1】:

    也许字符串末尾有一个换行符。

    您可以检查字符代码,以便您只转换实际上是小写字母的字符:

    for (i = 0; name[i] != '\0'; i = i + 1) {
      char c = name[i];
      if (c => 97 && c <= 122) {
        c -= 32;
      }
      printf("%c", c);
    }
    

    【讨论】:

    • 谢谢它绝对是一个解决方案,作为另一种选择,我记得 fgets() 在我完成输入名称后从输入中获取新行,我应该在 fgets 之后添加下一行() name[strlen(name)-1] = '\0'; 所以它可以删除我在转换时输入的新行,并将其视为空。
    • 我知道这个问题是老问题了;但是,我想强制每个人在此类操作中使用unsigned char,因为字符可能会在某些平台上签名。
    【解决方案2】:
    void read_chararray(char in_array[], int* Length)
    {
        int Indx = 0, Indx2 = 0, Indx3 = 0;  // int declarations for indexs of some loops
        char cinput = { 0 }, word[255] = { 0 }, word2[255] = { 0 }; // declaration of cinput and first char array before punctiation removed
    
        for (Indx = 0; (cinput = getchar()) != '\n'; Indx++) { // Loop for getting characters from user stop at <enter>
            word[Indx] = cinput;                    // Placing char into array while changing to lowercase
        }   
        
        Indx2 = Indx;                               // Set Indx2 to Indx for loop operation
        for (Indx = 0; Indx < Indx2; Indx++) {      // Loop to check and replace upper characters with lower
            cinput = word[Indx];
            if (cinput >= 65 && cinput <= 90) {     // If cinput is within the ASCII range 65 and 90, this indicates upper characters
                cinput += 32;                       // Add 32 to cinput to shift to the lower character range within the ASCII table
                in_array[Indx] = cinput;            // Input new value into array pointer
            }
            else if (cinput >= 97 && cinput <= 122) // scans if character are lower ASCII, places them in array irraticating punctuation and whitespce
                in_array[Indx] = cinput;            // Input remaining lower case into array pointer
        }
        
        *Length = Indx;                              // final size of array set to Length variable for future use
    }
    

    【讨论】:

      【解决方案3】:
      #include<stdio.h>
      
      void upper(char);
      
      void main()
      {
          char ch;
          printf("\nEnter the character in lower case");
          scanf("%c", &ch);
          upper(ch);
      }
      
      void upper( char c)
      {
          printf("\nUpper Case: %c", c-32);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-28
        • 1970-01-01
        • 2013-02-04
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-16
        相关资源
        最近更新 更多