【问题标题】:How to change uppercase letter to its next letter keeping lowercase letters unchanged in array?如何将大写字母更改为下一个字母,使数组中的小写字母保持不变?
【发布时间】:2021-12-31 11:27:22
【问题描述】:

我试图解决一个关于如何将大写字母更改为下一个字母并保持小写字母不变的问题。起初我试图检查字符串中是否存在任何大写字母,然后我应该尝试将其更改为小写字母。但我认为我的代码有问题。这是我的一段代码:

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

int main()
{
   char alpha[100];

   printf("Enter String: ");
   gets(alpha);

   for (int i = 0; alpha[i] != 0; i++)
   {
       if (alpha[i] >= 'A' && alpha[i] <= 'Z')
       {
           //upperL++;

           if (alpha[i] >= 65 && alpha[i] <= 90)
           {
               printf("\n %c", alpha + 1);
           }
           else if (alpha[i] >= 97 && alpha[i] <= 122)
           {
               printf("\n %c", alpha + 1);
           }
           else if (alpha[i] == 90)
           {
               printf("\n %c", 65);
           }
           else if (alpha[i] == 122)
           {
               printf("\n %c", 122);
           }
           else
           {
               printf("\n %c", alpha);
           }
       }
   }

   getch();

   return 0;
}

【问题讨论】:

  • 函数顶部的 if(alpha[i] &gt;= 'A' &amp;&amp; alpha[i] &lt;= 'Z')alpha[i] 置于 65 到 90 之间。因此其中的所有其他检查都是无用的。
  • 你的逻辑没有道理。在内部块中,第一个条件if (alpha[i] &gt;= 65 &amp;&amp; alpha[i] &lt;= 90) 将始终为真,因为它与外部if 的条件完全相同。所有其他的内在条件永远不会是真的,也可能不存在。
  • 你有一堆逻辑错误,例如,你正在检查if (alpha[i] &gt;= 65 &amp;&amp; alpha[i] &lt;= 90),然后在下面,你正在检查else if(alpha[i] == 90),当然这永远不会发生,因为你'已经检查过了。
  • 那么,alpha[i] &gt;= 97 &amp;&amp; alpha[i] &lt;= 122 是小写字母,这与您开始的问题相矛盾。
  • 简而言之,关于I think that there's something wrong in my code,下面的视频很好地总结了它:youtube.com/watch?v=TE3EZdoWIUw

标签: c string


【解决方案1】:

您的大写测试适用于 ASCII 编码,但您更改字符的大小写不正确:printf("\n %c", alpha + 1); 将指针传递给数组中的第二个字符。你应该写:

    printf("\n %c", alpha[i] + 1);

您还应该处理非大写字符,因为在初始测试中没有else 子句,它们会被忽略。

您需要特例 'Z',但您的测试失败,因为 'Z' 已由测试 (alpha[i] &gt;= 65 &amp;&amp; alpha[i] &lt;= 90) 处理。使用硬编码的 ASCII 值难以阅读且不可移植。

其他测试总是失败,因为它们嵌套在 test first test for 大写字符中。

还请注意,您不应使用gets(),它不能安全使用,并且已从最新版本的 C 标准中删除。请改用fgets()

这是修改后的版本:

#include <stdio.h>

int main() {
    char alpha[100];

    printf("Enter String: ");
    if (fgets(alpha, sizeof alpha, stdin)) {
        for (int i = 0; alpha[i] != '\0'; i++) {
            if (alpha[i] >= 'A' && alpha[i] < 'Z') {
                alpha[i] += 1;
            } else
            if (alpha[i] == 'Z') {
                alpha[i] = 'A';
            }
        }
        fputs(alpha, stdout);
    }
    getchar();

    return 0;
}

上面的代码假设大写字母被编码为一个连续的块。这就是今天几乎无处不在的 ASCII 的情况。下面是一个可以处理其他字符集的便携版本:

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

int main() {
    char alpha[100];
    const char *upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZA";

    printf("Enter String: ");
    if (fgets(alpha, sizeof alpha, stdin)) {
        for (int i = 0; alpha[i] != '\0'; i++) {
            const char *p = strchr(upper, alpha[i]);
            if (p != NULL) {
                alpha[i] = p[1];
            }
        }
        fputs(alpha, stdout);
    }
    getchar();

    return 0;
}

【讨论】:

  • 感谢您的帮助。但是我有一个问题,如果我想将大写字母打印成下一个小写字母而不改变其余的小写字母,我这样做了 - const char *lower = "abcdefghijklmnopqrstuvwxyza"; . . . const char *p = strchr(lower, alpha[i]); // 而不是 strchr(lower alpha[i]);但我没有得到我想要的实际结果。
  • @Md.AsifImityajChowdhury:你应该把alpha[i] = p[1]改成alpha[i] = tolower(p[1])
【解决方案2】:

您应该将功能放在一个函数中,从而给它一个名称,然后它将分配与环境的其余部分隔离开来。

举个例子这个:

#include <assert.h>
#include <ctype.h>

void nextUpperCase(char *str) {
    assert(str && "function `nextUpperCase' not designed to handle null pointers");
    for (; *str != '\0'; ++str)
        if (isupper(*str) && !isalpha(++*str))
            *str = 'A';
}

无需手动输入即可轻松测试:

// #include <locale.h> Needed for locale other than "C"
#include <stdio.h>

int main() {
    // setlocale(LC_ALL, "C");  // this is implicitly done before main is entered
    char buf[] = "Hello, Zee World!";
    puts(buf);  // prints `Hello, Z World!'
    nextUpperCase(buf);
    puts(buf);  // prints `Iello, A Xorld!'
    nextUpperCase(NULL);  // assertion
    return 0;
}

这个特定的解决方案假定“C”语言环境。如果使用任何其他语言环境,则此建议的“下一步”解决方案假定“A”...“Z”没有孔。对于其他语言环境,“下一个”定义需要一些非常不同的东西。

【讨论】:

    【解决方案3】:

    试试这个代码:

     #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    {
       char alpha[100];
    
       printf("Enter String: ");
       gets(alpha);
    
       for (int i = 0; alpha[i] != 0; i++)
       {
           if (alpha[i] >= 'A' && alpha[i] <= 'Z')
           {
               //upperL++;
    
               if (alpha[i] >= 65 && alpha[i] <= 90)
               {
                   printf("\n %c", alpha + 1);
               }
               else if (alpha[i] >= 97 && alpha[i] <= 122)
               {
                   printf("\n %c", alpha + 1);
               }
               else if (alpha[i] == 90)
               {
                   printf("\n %c", 65);
               }
               else if (alpha[i] == 122)
               {
                   printf("\n %c", 122);
               }
               else
               {
                   printf("\n %c", alpha);
               }
           }
       }
    
       getch();
    
       return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2015-08-07
      • 2020-08-24
      • 2015-07-28
      • 1970-01-01
      相关资源
      最近更新 更多