用c语言编写:输入一段英文,将里面每个单词的字母翻转并输出结果
时间: 2023-03-28 22:04:38 浏览: 76
您好,我是 CSDN 开发的 C 知道。以下是回答您的问题:
代码如下:
```
#include <stdio.h>
#include <string.h>
void reverse(char *start, char *end) {
char temp;
while (start < end) {
temp = *start;
*start++ = *end;
*end-- = temp;
}
}
int main() {
char str[100];
printf("请输入一段英文:\n");
fgets(str, 100, stdin);
int len = strlen(str);
int i = ;
while (i < len) {
int j = i;
while (str[j] != ' ' && str[j] != '\n') {
j++;
}
reverse(&str[i], &str[j-1]);
i = j + 1;
}
printf("翻转后的结果为:\n%s", str);
return ;
}
```
希望能够帮到您!
相关问题
用C语言写:输入一段文字,输出有多少个单词
好的,以下是用C语言实现输入一段文字,输出有多少个单词的代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char text[1000];
int i, count = 0, word = 0;
printf("请输入一段文字:\n");
fgets(text, 1000, stdin);
for (i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) {
word = 1;
} else {
if (word == 1) {
count++;
word = 0;
}
}
}
if (word == 1) {
count++;
}
printf("这段文字中有 %d 个单词。\n", count);
return 0;
}
```
以上代码中,我们使用了一个字符串数组 `text` 来存储用户输入的一段文字,使用 `fgets` 函数来获取用户输入。然后我们遍历整个字符串,如果遇到字母,就说明进入了一个单词,将 `word` 的值设为 1。如果遇到非字母字符,就说明一个单词结束了,如果此时 `word` 的值为 1,说明刚才进入了一个单词,将 `count` 的值加 1,并将 `word` 的值设为 0。最后如果 `word` 的值为 1,说明最后一个单词还没有结束,将 `count` 的值再加 1。最后输出单词的数量即可。
希望能对你有所帮助!
编写一个简单的C语言程序:请输入下列字符串,并提取每个单词的首字母
好的,以下是一个简单的 C 语言程序,用于输入指定的字符串,然后提取每个单词的首字母:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[] = "Hello World, I am a C program!";
char *p = strtok(str, " ,!"); // 分割字符串为单词
while (p != NULL) {
printf("%c", toupper(p[0])); // 输出单词的首字母
p = strtok(NULL, " ,!"); // 继续分割字符串
}
return 0;
}
```
该程序使用了标准库函数 strtok 来将输入的字符串分割为单词,然后使用 toupper 函数将每个单词的首字母转换为大写字母,并输出到屏幕上。
需要注意的是,程序假定单词之间是使用空格符、逗号或感叹号分隔的。如果输入的字符串中使用了其他的分隔符,需要修改 strtok 函数的第二个参数。另外,由于 strtok 函数会修改输入的字符串,因此需要将输入的字符串复制到一个临时的字符数组中,以避免原始输入的字符串被修改。