如何在C中小写一个字符串? [英] How do I lowercase a string in C?

查看:66
本文介绍了如何在C中小写一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C语言中将大小写混合的字符串转换为小写的字符串?

How can I convert a mixed case string to a lowercase string in C?

推荐答案

它在标准库中,这是我看到的实现这种功能的最直接的方法.所以是的,只需遍历字符串并将每个字符转换为小写即可.

It's in the standard library, and that's the most straight forward way I can see to implement such a function. So yes, just loop through the string and convert each character to lowercase.

像这样的琐碎事:

#include <ctype.h>

for(int i = 0; str[i]; i++){
  str[i] = tolower(str[i]);
}

或者,如果您更喜欢一种衬板,则可以使用J.F. Sebastian的这种衬板:

or if you prefer one liners, then you can use this one by J.F. Sebastian:

for ( ; *p; ++p) *p = tolower(*p);

这篇关于如何在C中小写一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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