#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char str[100]; printf("Please enter a sentence: "); fgets(str, 100, stdin); char *token = strtok(str, " "); while (token != NULL) { int len = strlen(token); if (len > 1 && isalpha(token[0])) { putchar(toupper(token[len-1])); for (int i = len-2; i >= 1; i--) { putchar(token[i]); } putchar(token[0]); } else { printf("%s", token); } token = strtok(NULL, " "); } return 0; }
时间: 2024-04-29 18:19:44 浏览: 187
这段代码是一个简单的字符串处理程序,其作用是将输入的句子进行反转和大小写转换。
具体来说,程序会先提示用户输入一个句子,然后使用 fgets() 函数读取输入的字符串。接着,程序使用 strtok() 函数将字符串按空格分割成单词,并对每个单词进行处理。
如果单词的长度大于 1 且第一个字符是字母,程序会将单词的最后一个字符转换为大写,并将单词首尾反转输出。如果单词不符合这个条件,则直接输出该单词。
最后,程序返回 0,表示执行成功。
相关问题
#include <stdio.h>中小写转大写
`#include <stdio.h>` 是 C 语言中预处理指令的一部分,它引入了标准输入输出库(stdio),而小写转大写通常是在字符串操作中需要的。在 C 语言中,如果你想要将字符串中的所有字符从小写转换为大写,可以先定义一个函数,比如使用标准库提供的 `toupper()` 函数,它是 `ctype.h` 库的一部分,用于转换字符。
下面是一个简单的示例,展示如何在 C 中实现这个功能:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
// 自定义函数,接收字符串并返回转换后的字符串
char* to_uppercase(char *str) {
int len = strlen(str);
for (int i = 0; i < len; ++i) {
str[i] = toupper((unsigned char)str[i]);
}
return str;
}
int main() {
char lowercase_str[] = "hello, world!";
char uppercase_str[50];
strcpy(uppercase_str, to_uppercase(lowercase_str));
printf("Original: %s\n", lowercase_str);
printf("Uppercased: %s\n", uppercase_str);
return 0;
}
```
在这个例子中,`to_uppercase` 函数遍历字符串中的每个字符,并调用 `toupper` 将其转换成大写。然后在主函数中,我们将小写字符串传递给这个函数,得到的结果存储在另一个数组中。
不用#include <ctype.h> 实现
如果你不想使用`<ctype.h>`库来检查字符是否为小写字母,可以手动编写一段代码来判断。下面是一个不需要`ctype.h`的版本:
```c
#include <stdio.h>
#include <string.h>
// 定义计数小写字母的函数
int CountLowerCase(char* str) {
int count = 0;
for (; *str != '\0'; str++) { // 遍历字符串直到结束
if (*str >= 'a' && *str <= 'z') { // 判断是否是小写字母
count++;
}
}
return count; // 返回小写字母的总数
}
int main() {
char s1[10000], s2[10000];
printf("请输入第一个字符串(小写字符计数):");
fgets(s1, sizeof(s1), stdin);
s1[strcspn(s1, "\n")] = '\0';
printf("请输入第二个字符串(小写字符计数):");
fgets(s2, sizeof(s2), stdin);
s2[strcspn(s2, "\n")] = '\0';
int count1 = CountLowerCase(s1);
int count2 = CountLowerCase(s2);
printf("字符串s1中的小写字母个数为:%d\n", count1);
printf("字符串s2中的小写字母个数为:%d\n", count2);
return 0;
}
```
在这里,我们通过比较ASCII值范围的方式来判断字符是否在小写字母范围内。`*str >= 'a'` 和 `*str <= 'z'` 分别表示字符是小写字母的开始和结束位置。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)