• c和python解决各种字符串反转问题的不同思路


      问题1:反转字符串,从头到尾反转整个字符串,就像在镜子中看到的反转。

      输入:Hello World

      输出:dlroW olleH

      这个问题比较简单一些,主要是设置两个指针分别指向头尾,依次交换直到相遇。

     1 #include <stdio.h>
     2 void reverseString(char *p);
     3 int main()
     4 {    
     5     char str[]="Hello World";
     6     reverseString(str);
     7     printf("%s",str);
     8     return 0;
     9 }
    10 void reverseString(char *p)
    11 {
    12     char *pEnd = p;
    13     for (; *pEnd!=''; pEnd++);
    14     --pEnd;
    15 
    16     char temp;
    17     for (; p < pEnd; ++p, --pEnd)
    18     {
    19         temp = *p;
    20         *p = *pEnd;
    21         *pEnd = temp;
    22     }
    23 }

      python的实现有多种方法,可以直接s[::-1];也可以利用reduce(lambda x,y:y+x,s)。

      当然也可以转化为列表再反转,然后再转化我字符串:

    1 def reverse_str(s):
    2     ls=list(s)
    3     ls.reverse()
    4     str="".join(ls)
    5     return str

    问题2:字符串中以空格隔开的单词反转,但是整个字符串不反转。

      输入:hello world

      输出:olleh dlrow

     1 #include <stdio.h>
     2 void reverseword(char *p);
     3 void reverse(char *pStart, char *pEnd);
     4 int main()
     5 {    
     6     char str[]=" Hello World";
     7     reverseword(str);
     8     printf("%s",str);
     9     return 0;
    10 }
    11 void reverseword(char* p)
    12 {
    13     char *p1 = p;
    14     while (*p1 != '')
    15     {
    16         // Skip all blank characters.
    17         for (; *p1==' '; p1++) ;
    18 
    19         char *p2 = p1;
    20 
    21         // Let p2 points to the end of the current word.
    22         for (; *p2!=' ' && *p2!=''; p2++);
    23         --p2;
    24 
    25         // Reverse the current word.
    26         reverse(p1, p2);
    27 
    28         ++p2;
    29         p1 = p2;
    30     }
    31 }
    32 void reverse(char *pStart, char *pEnd)
    33 {
    34     for (; pStart < pEnd; ++pStart, --pEnd)
    35     {
    36         char temp = *pStart;
    37         *pStart = *pEnd;
    38         *pEnd = temp;
    39     }
    40 }

      python的实现(暂时只想到一个复杂的做法)(后来证明这个解法有很大缺陷):

     1 def reverse_str(s):
     2     ls=list(s)
     3     flag=[]
     4     for i in range(len(ls)):
     5         if ls[i]!=' ':
     6             flag.append(i)
     7     
     8     loc=[]
     9     for i in range(len(flag)):
    10         if flag[i-1]!=flag[i]-1:
    11             if i==0:
    12                 loc.append(i)
    13                 loc.append(len(flag)-1)
    14             else :
    15                 loc.append(i-1)
    16                 loc.append(i)
    17     loc.append(loc[1])
    18     del loc[1]
    19     
    20     newloc=[]
    21     for i in loc:
    22         newloc.append(flag[i])
    23         
    24     for i in range(0,len(newloc),2):
    25         newword=ls[newloc[i]:newloc[i+1]+1]
    26         newword.reverse()
    27         ls[newloc[i]:newloc[i+1]+1]=newword
    28         
    29     ls=''.join(ls)
    30     print ls
    31 if __name__=='__main__':
    32     s="  Hello   W orld  "
    33     reverse_str(s)   

      上面的解法肯定有问题,后来想一想是因为对python的用法不熟悉,还是用c/c++的思路来解决问题。接着想出了python思考方式的解决方法。

      下面是解法:

     1 def reverse_str(s):
     2     ls=s.split(' ')
     3     newls=[]
     4     for c in ls:
     5         list(c).reverse()
     6         newls.append(c)  
     7     newstr=' '.join(newls)
     8     print newstr
     9 if __name__=='__main__':
    10     s="  Hello     g W orld  "
    11     reverse_str(s)

      问题3:反转字符串中单词次序,单词本身不反转

      输入:hello world

      输出:world hello

     1 void reverseWords(char *s)
     2 {
     3     char *word_begin = NULL;
     4     char *temp = s; /* temp is for word boundry */
     5  
     6     /*STEP 1 of the above algorithm */
     7     while( *temp )
     8     {
     9         /*This condition is to make sure that the string start with
    10           valid character (not space) only*/
    11         if (( word_begin == NULL ) && (*temp != ' ') )
    12         {
    13             word_begin=temp;
    14         }
    15         if(word_begin && ((*(temp+1) == ' ') || (*(temp+1) == '')))
    16         {
    17             reverse(word_begin, temp);
    18             word_begin = NULL;
    19         }
    20         temp++;
    21     } /* End of while */
    22  
    23     /*STEP 2 of the above algorithm */
    24     reverse(s, temp-1);
    25 }

      python中的简单实现(由于这种python解法没有考虑空格,所以较为简单):

    str = ' '.join(s.split( )[::-1]) 
  • 相关阅读:
    20155322 2017-2018-1《信息安全系统设计》第七周学习总结
    20155322 2017-2018-1《信息安全系统设计》实验二:固件程序设计
    20155322 2017-2018-1《信息安全系统设计》第六周学习总结
    20155322 2017-2018-1《信息安全系统设计》第六周 课下作业
    01--DNS服务器1
    华为lab-rs-v1-2.5_流量优化
    华为lab-rs-v1-2.4_OSPF提升
    华为lab-rs-v1-2.3_OSPF基础
    华为lab-rs-v1-2.2_RIP基础
    华为lab-rs-v1-2.1_IP基础
  • 原文地址:https://www.cnblogs.com/lkprof/p/3162309.html
Copyright © 2020-2023  润新知