鱼C论坛

 找回密码
 立即注册
查看: 1124|回复: 3

[已解决]字符逆序

[复制链接]
发表于 2020-12-22 11:15:25 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。
输入格式:
输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。
输出格式:
在一行中输出逆序后的字符串。
输入样例:
Hello World!
输出样例:
!dlroW olleH
最佳答案
2020-12-22 11:32:06
本帖最后由 jackz007 于 2020-12-22 11:33 编辑
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         char c , s[80]                                                             ;
  5.         int i , m                                                                  ;
  6.         for(m = 0 ; m < 80 && (c = getchar()) != '\n' ; s[m ++] = c , s[m] = '\0') ;
  7.         for(i = 0 ; i < m / 2 ; i ++) {
  8.                 c = s[i]                                                           ;
  9.                 s[i] = s[m - 1 - i]                                                ;
  10.                 s[m - 1 - i] = c                                                   ;
  11.         }              
  12.         printf("%s\n" , s)                                                         ;
  13. }
复制代码

        编译、运行实况
  1. D:\0002.Exercise\C>g++ -o x x.c

  2. D:\0002.Exercise\C>x
  3. hello , world !
  4. ! dlrow , olleh

  5. D:\0002.Exercise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-22 11:32:06 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-12-22 11:33 编辑
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         char c , s[80]                                                             ;
  5.         int i , m                                                                  ;
  6.         for(m = 0 ; m < 80 && (c = getchar()) != '\n' ; s[m ++] = c , s[m] = '\0') ;
  7.         for(i = 0 ; i < m / 2 ; i ++) {
  8.                 c = s[i]                                                           ;
  9.                 s[i] = s[m - 1 - i]                                                ;
  10.                 s[m - 1 - i] = c                                                   ;
  11.         }              
  12.         printf("%s\n" , s)                                                         ;
  13. }
复制代码

        编译、运行实况
  1. D:\0002.Exercise\C>g++ -o x x.c

  2. D:\0002.Exercise\C>x
  3. hello , world !
  4. ! dlrow , olleh

  5. D:\0002.Exercise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 11:49:28 | 显示全部楼层
int main()
{
        printf("cout string:");
        char str[80],temp;
        gets(str);
        int num = strlen(str);
        for (int i = 0; i < num/2 ; i++)
        {
                temp = str[i];
                str[i] = str[num - i - 1];
                str[num - i - 1] = temp;
        }
        puts(str);
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 11:58:02 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int main()
  5. {
  6.     char a[80];
  7.     int i,n;
  8.     gets(a);
  9.     i=strlen(a);
  10.     for(i-1; i>0; i--)
  11.     {
  12.         putchar(a[i-1]);
  13.     }
  14. }


复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-7 12:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表