2301_77585843 2023-06-03 19:50 采纳率: 80%
浏览 17
已结题

C语言中ctype.h函数的用法,请问这两个代码的区别是什么,第二个代码尝试将convertToUppercase函数写入main函数,写入后发生错误

C语言中ctype.h函数的用法,请问这两个代码的区别是什么,为什么第一个代码正常运行,第二个代码尝试将convertToUppercase函数写入main函数,写入后发生错误,无法运行。

第一个代码如下:

#include <stdio.h>
#include<ctype.h>
#include<string.h>
void convertToUppercase(char*sPtr);
int main() {
   char string[20]="happy birthday";
   convertToUppercase(string);
   printf("%s",string);
   return 0;
}
void convertToUppercase(char*sPtr){
   while(*sPtr!='\0'){
       if(islower(*sPtr))
         *sPtr=toupper(*sPtr);
       ++sPtr;
   }
}

正常运行,输出:HAPPY BIRTHDAY

第二个代码如下:

#include <stdio.h>
#include<ctype.h>
#include<string.h>
int main() {
   char string[20]="happy birthday";
   while(*string!='\0'){
       if(islower(*string))
         *string=toupper(*string);
       ++string;
   }
   printf("%s",string);
   return 0;
}

无法运行

  • 写回答

2条回答 默认 最新

  • P2441M 2023-06-03 20:19
    关注
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main()
    {
        char string[20] = "happy birthday";
        char *ptr = string;  // 如果是函数,实际上传入的是指向数组开头的指针。
        while (*ptr != '\0')
        {
            if (islower(*ptr))
                *ptr = toupper(*ptr);
            ++ptr;
        }
        printf("%s", string);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月11日
  • 已采纳回答 6月3日
  • 创建了问题 6月3日

悬赏问题

  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了