字符串逆序输出函数c
时间: 2024-06-14 22:03:41 浏览: 7
以下是一个用C语言实现字符串逆序输出的函数的例子:
```c
#include <stdio.h>
#include <string.h>
void stringNx(char a[]) {
int length = strlen(a);
for (int i = length - 1; i >= 0; i--) {
printf("%c", a[i]);
}
printf("\n");
}
int main() {
char str[] = "hello world";
printf("正序字符串:%s\n", str);
printf("逆序字符串:");
stringNx(str);
return 0;
}
```
运行结果:
```
正序字符串:hello world
逆序字符串:dlrow olleh
```
相关问题
字符串逆序函数,C语言
C语言中实现字符串逆序可以通过遍历字符串并交换相应位置上的字符来实现。这里给出一个示例代码实现:
```c
#include <stdio.h>
#include <string.h>
void reverse_string(char *s) {
int length = strlen(s);
char temp;
for (int i = 0; i < length / 2; i++) {
temp = s[i];
s[i] = s[length - i - 1];
s[length - i - 1] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
reverse_string(str);
printf("Reversed string: %s", str);
return 0;
}
```
这个程序中,我们首先使用 `strlen()` 函数获取字符串的长度,然后使用一个循环来遍历字符串。在循环中,我们交换字符串中对称位置上的字符,直到到达字符串中间位置。最后,我们输出翻转后的字符串。例如,如果输入字符串为 "hello",则输出结果为 "olleh"。注意,不要使用 `gets()` 函数,这个函数已经被废弃了,使用 `fgets()` 更安全。
字符串逆序输出c语言
下面是一种简单的实现方法:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
printf("逆序输出字符串:");
for(int i=strlen(str)-1; i>=0; i--) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
```
该程序首先从用户输入中读取一个字符串,然后使用 `strlen` 函数获取该字符串的长度。接着,程序使用一个循环从字符串的最后一个字符开始,依次输出每一个字符,直到第一个字符为止。最后,程序输出一个换行符并返回 0。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)