tolower() 和 toupper() 不起作用 [英] tolower() and toupper() aren't working

查看:36
本文介绍了tolower() 和 toupper() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在这里:

char* kropecka(char* tab)
{
    int a=0,b=0;
    char* zwr;
    zwr=(char*)malloc(30*sizeof(char));

    for(a;strlen(tab);a++)
    {
        if(tab[a]!='.')
        {
            if(isupper(tab[a]))
                zwr[b]=tolower(tab[a]);
            if(islower(tab[a]))
                zwr[b]=toupper(tab[a]);
            b++;
        }
    }
    zwr[b]='\0';
    return zwr;
}

没有错误、警告或类似的东西.但是当我给他一些字符串时程序崩溃了:

There is no errors, warnings or something like this. But program crashed when I give him some string:

--------------------------- Microsoft Visual C++ 调试库--------------------------- 调试断言失败!

--------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed!

程序:...s\Visual Studio2010\Projects\C_homework\Debug\C_homework.exe 文件:f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c 行:56

Program: ...s\Visual Studio 2010\Projects\C_homework\Debug\C_homework.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c Line: 56

表达式:(无符号)(c + 1) <= 256

Expression: (unsigned)(c + 1) <= 256

有关您的程序如何导致断言失败的信息,请参阅有关断言的 Visual C++ 文档.

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(按重试调试应用程序)

(Press Retry to debug the application)

编译器:Visual Studio 2010包含的库:stdio.h、string.h、ctype.h、stdlib.h(用于 main() 中的 system() 函数).

Compilator: Visual Studio 2010 Included libary: stdio.h, string.h, ctype.h, stdlib.h (for system() function in main() ).

推荐答案

这将永远运行:

for(a;strlen(tab);a++)

我想你的意思是:

for(a;a < strlen(tab);a++)

或者更好(因为 strlen 是 O(n)):

Or better (because strlen is O(n)):

for(;tab[a];a++)

这篇关于tolower() 和 toupper() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆