C语言字符串转小写:tolower() 函数示例

本文将介绍如何使用 C 语言中的 tolower() 函数将字符串转换为小写字母。

代码示例:

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

void toLower(char *str) {
    int i = 0;
    while(str[i]) {
        str[i] = tolower(str[i]);
        i++;
    }
}

int main() {
    char str[] = 'HELLO WORLD';
    printf('Before: %s\n', str);
    toLower(str);
    printf('After: %s\n', str);
    return 0;
}

解释:

  1. tolower() 函数: tolower() 函数定义在 ctype.h 头文件中,它接收一个字符作为参数,并将该字符转换为小写字母。
  2. 循环: 循环遍历输入字符串中的每个字符,将每个字符都使用 tolower() 函数转换为小写字母。

注意:

  • 代码中的 toLower() 函数将修改传入的字符串,使其变为小写。
  • 你可以根据需要修改代码中的字符串,并运行代码查看结果。

希望本文能帮助你理解如何在 C 语言中将字符串转换为小写字母。

C语言字符串转小写:tolower() 函数示例

原文地址: https://www.cveoy.top/t/topic/lZsk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录