【问题标题】:c - How to make whole string uppercase, if it contains numbers?c - 如果包含数字,如何使整个字符串大写?
【发布时间】:2016-10-29 11:38:40
【问题描述】:

所以我需要将字符串设为大写 ​​+ 从中删除空格。但是它不起作用,如果字符串包含数字,则最终打印会打印一些非 ascii 字符。我应该如何使它工作?我尝试使用函数isalpha()isdigit() 来做,但结果是一样的。

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

int main(){
    int i = 0;
    char c[100];
    char str[] = "Hello8 world";

    while(str[i]){
        if (str[i]!=' '){
            if (str[i] >= '0' && str[i] <= '9'){
                c[i]=str[i];
            }
            else{
                c[i]=(toupper(str[i]));
            }
        i++;
        }
   }
   printf("%s", c);

   return(0);
}

【问题讨论】:

  • "但是它不起作用,如果字符串包含数字,最终打印会打印一些非 ascii 字符。" -- 那不是你的代码在做什么。对于您在问题中输入的字符串,您的代码进入了一个无限循环。请确定您需要帮助的哪些代码。是这个代码吗?然后使问题文本与代码的行为相匹配。是其他代码吗?然后发布一些其他代码。
  • 您确实不需要需要将数字设为特殊情况。 toupper 只更改字母字符,其他所有字符保持不变。
  • @hdv 你是对的,我有点期待应该不让空间进入新字符串的部分工作。无限循环是由 if (str[i]!=' '){ 引起的,你有什么建议?
  • @AnetaKožoušková,你为什么不使用按位运算?它快速而优雅。

标签: c string c-strings


【解决方案1】:

这里有几个问题。首先,您只在if 语句中推进i,因此一旦遇到空格,您的问题就会陷入endlees 循环。 其次,您假设源字符串和目标字符串中的索引相同 - 这是不正确的,因为您正在跳过空格。这将导致目标字符串保留未初始化的内存 - 在某些平台上可能是 \0,而在其他平台上可能只是随机垃圾。相反,您应该维护两个索引计数器,一个用于源,一个用于目标,并在完成后在目标末尾显式设置 \0

int i = 0;
int j = 0; /* Target string index */
char c[100];
char str[] = "Hello8 world";

while(str[i]){
    if (str[i]!=' '){ 
        if (str[i] >= '0' && str[i] <= '9'){
            c[i]=str[i];
        }
        else{
            c[j]=(toupper(str[i]));
        }
        j++; /* Target index advance only when it's used */
    }
    i++; /* Source index advanced regardless */
}
c[j] = '\0'; /* Handle the string termination */
printf("%s\n", c);

【讨论】:

    【解决方案2】:

    1-您不需要创建另一个字符串。 2- toupper() 函数不会将数字转换为其他值。 3-如果你找到一个空格,你只需要拉出字符串的其余部分。

    int i = 0, j;
    char str[] = "Hello8 world";
    
    while(str[i]!='\0')
    {
        if(str[i]==' ')
        {   
            for(j=i;str[j]!='\0';j++)
            {
                str[j]=str[j+1];
            }
        }
        else
        {
            str[i]=toupper(str[i]);
            i++;
        }
    
    }
    
    printf("%s\n", str);
    
    return 0;
    

    【讨论】:

      【解决方案3】:

      while 循环之后,添加c[i] = '\0';

      这会将字符数组转换为字符串(根据定义,字符串必须以\0 作为终止符)。

      【讨论】:

        【解决方案4】:

        为什么不使用 for 循环,像这样:

        #include <stdio.h>
        #include <stdlib.h>
        #include <ctype.h>
        
        #define MAXCHAR 100
        
        int
        main(void) {
            char str[] = "Hello8 world";
            char upper[MAXCHAR];
            int str_pos = 0, i;
        
            for (i = 0; str[i]; i++) {
                if (!isspace(str[i])) {
                    upper[str_pos++] = toupper(str[i]);
                }
            }
            upper[str_pos] = '\0';
        
            printf("%s\n", upper);
        
            return 0;
        }
        

        【讨论】:

        • 你可以试试这个更简单的方法@Aneta Kožoušková
        猜你喜欢
        • 2017-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        相关资源
        最近更新 更多