zoukankan      html  css  js  c++  java
  • 数据结构与算法之逆序字符串

    1

    #include<stdio.h>
    #include<string.h> 
    int main()
    {
    	char ch[]="abcdefg";
    	int len=strlen(ch);
    	char *p=ch,*q=ch+len-1,temp;
    	while(p<q)
    	{
    		temp=*p;
    		*p=*q;
    		*q=temp;
    		++p;
    		--q;
    	}
    	printf("%s",ch);
    }
    

    2

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char *ch="abcdefg";
    	char buf[]="abcdefg" 
    	int len=strlen(ch);
    	char*p=buf,*q=buf+len-1,temp;
    	while(p<q)
    	{
    		temp=*p;
    		*p=*q;
    		*q=temp;
    		++p;
    		--q;
    	}
    	printf("%s",buf);
    }
    

    3

    //递归方法
    #include<stdio.h>
    int reverse(char*p)
    {
    	if(p==NULL)
    	return -1;
    	if(*p=='')
    	return 0;
    	reverse(p+1);
    	printf("%c",*p);
    }
    int main()
    {
    	char *p="abcdefg";
    	reverse(p);
     } 
    

    4

    #include<stdio.h>
    #include<string.h>
    int reverse(char*p,char *buf)

    {

       int i=0;
    
       if(p==NULL)
    
              return -1;
    
       if(*p=='')
    
              return 0 ;
    
       reverse(p+1,buf);
    
       strncat(buf,p,1);
    

    }
    int main()

    {

              char *p="abcdefg";
    
              char buf[1024]={0};
    
              reverse(p,buf);
    
              printf("%s",buf);
    

    }

  • 相关阅读:
    超级迷宫我的计划表
    不敢死队
    Let the Balloon Rise
    Hangover
    汉诺塔系列2
    Tri Tiling(递推)
    Tiling(递推,高精度)
    Color Me Less
    I Think I Need a Houseboat(圆计算)
    Kbased Numbers(递推)
  • 原文地址:https://www.cnblogs.com/AmosAlbert/p/12832363.html
Copyright © 2011-2022 走看看