C语言代码 将单词中大写字母转小写字母并输出单词
时间: 2024-05-03 07:23:20 浏览: 19
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char word[100];
printf("请输入一个单词:");
scanf("%s", word);
for (int i = 0; word[i] != '\0'; i++) {
word[i] = tolower(word[i]);
}
printf("转换后的单词为:%s", word);
return 0;
}
```
注:该代码使用了 `<ctype.h>` 中的 `tolower()` 函数,将大写字母转换为小写字母。
相关问题
字符串单词首字母大写字母转小写C语言
要将字符串中的单词首字母大写字母转换为小写字母,可以使用C语言中的标准库函数tolower()和toupper()。以下是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void convertToLower(char *str) {
int length = strlen(str);
int i;
// 将字符串中的单词首字母转换为小写字母
for (i = 0; i < length; i++) {
if (i == 0 || str[i-1] == ' ') {
str[i] = tolower(str[i]);
}
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
// 移除字符串末尾的换行符
if (str[strlen(str) - 1] == '\n') {
str[strlen(str) - 1] = '\0';
}
convertToLower(str);
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
这段代码会要求你输入一个字符串,然后将字符串中的单词首字母转换为小写字母,并输出转换后的字符串。请注意,这里假设单词之间是用空格分隔的。如果你的字符串中使用了其他分隔符,请根据需要进行修改。
c语言单词首字母大写
以下是两种C语言单词首字母大写的例子:
1.在一行中输出已输入的字符,其中所有单词的首字母已改为大写。
```c
#include <stdio.h>
int main(int argc, char *argv[]) {
char ch;
ch=getchar();
int flag=1;//首字母大写标志
while(ch!='\n') {
if(ch>='a'&&ch<='z'&&flag==1)//小写字母并且有大写字母标志,则大写
{
ch=ch-32;
putchar(ch);
}
else {
putchar(ch);
}
if(ch==' ')//空格标志下一个字母要大写
flag=1;
else
flag=0;
ch=getchar();
}
return 0;
}
```
2.字符串首字母转换成大写简单实例
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
char input[] = "this is a book";
char output[256] = {'\0'};
int i = 0;
int j = 0;
int len = strlen(input);
for(i = 0; i < len; i++)
{
if(i == 0 || input[i - 1] == ' ')
{
output[j++] = toupper(input[i]);
}
else
{
output[j++] = input[i];
}
}
printf("%s\n", output);
return 0;
}
```