鱼C论坛

 找回密码
 立即注册
查看: 1947|回复: 2

[已解决]字符串逆序

[复制链接]
发表于 2021-10-28 17:00:50 | 显示全部楼层 |阅读模式
10鱼币

写一个递归算法来实现字符串逆序存储,要求不另设串存储空间。在turn函数中进行填写
#include <iostream>
#include<string.h>
using namespace std;
#define INITSTRLEN 100
typedef struct 
{ char *ch;
  int length;
 int strsize;
}String;
void initString(String &s)
{ s.ch = new char[INITSTRLEN ]; /*初始化串存储空间*/
  s.length = 0; /*初始化串长*/
  s.strsize = INITSTRLEN; /*初始化当前存储空间容量*/
}
void turn(String &s,int &i)//算法填写
{
        
        
        
        
        

}
void list(String s)
{
 printf("%s", s.ch);
 printf("\n");}
int main(){
  String s;int i=0;
  initString(s);
  printf("please input the string s:\n");
  scanf("%s", s.ch);
  s.length=strlen(s.ch);
  
  turn(s,i);
  list(s);
  delete(s.ch);
  return 0;
  }
最佳答案
2021-10-28 17:00:51

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-28 17:00:51 | 显示全部楼层    本楼为最佳答案   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-28 17:53:15 | 显示全部楼层
#include <iostream>
#include<string.h>
using namespace std;
#define INITSTRLEN 100
typedef struct 
{ char *ch;
  int length;
 int strsize;
}String;
void initString(String &s)
{ s.ch = new char[INITSTRLEN ]; /*初始化串存储空间*/
  s.length = 0; /*初始化串长*/
  s.strsize = INITSTRLEN; /*初始化当前存储空间容量*/
}
void turn(String &s,int &i)//算法填写
{
        char temp = s.ch[i];
        s.ch[i] = s.ch[s.length-i-1];
        s.ch[s.length-1-i] = temp;
        //cout<<s.ch[i]<<endl;
        i++;
        if(i<s.length/2)turn(s,i);
        
        

        
        

}
void list(String s)
{
 printf("%s", s.ch);
 printf("\n");}
int main(){
  String s;int i=0;
  initString(s);
  printf("please input the string s:\n");
  scanf("%s", s.ch);
  s.length=strlen(s.ch);
  
  turn(s,i);
  list(s);
  delete(s.ch);
  return 0;
  }
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 19:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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