關於C語言逆序字串的相關代碼!

來源:互聯網
上載者:User

解決問題的時間首先想到的是搜尋引擎,並不是大腦。現在的習慣都變完了!!

//此段代碼以解決以下方式的逆序</p><p>//原字串: who is your name;<br />//目標字元: ;eman ruoy si ohw</p><p>#include <iostream><br />using namespace std;<br />#define Max 200</p><p>int main()<br />{<br />char str[Max];<br />printf("請輸入文本:");<br />gets(str);</p><p> int len=0;<br />char *strlen=str;<br />char *left=str;<br />char temp;<br />while(*strlen++)<br />{<br />len++;<br />}<br />strlen-=2;<br />while(left!=strlen)<br />{<br />temp=*left;<br />*left++=*strlen;<br />*strlen--=temp;<br />}<br />printf("逆序後的字串為:");</p><p>for(int i=0;i<len;i++)<br />{<br />printf("%c",str[i]);</p><p>}<br />printf("/n");</p><p>system("pause");<br />return 0;<br />}

 

//此段代碼是以按單詞來逆序</p><p>#include <iostream><br />#include <assert.h><br />using namespace std;<br />#define Max 200</p><p>char* ReversedText(char *originalText)<br />{<br />int textLength = 0;<br />int wordCount = 0;<br />int wordlength = 0;<br />char reversingText[Max];<br />int i = 0;<br />int j = 0;</p><p>assert(originalText != NULL);<br />textLength = strlen(originalText);<br />reversingText[textLength] = '/0';<br />printf("輸入文本:/"%s/"/n", originalText);</p><p>while (i < textLength)<br />{<br />for (j = i; (j < textLength) && (isalnum(originalText[j]) || ('-' == originalText[j])); j++)<br />{<br />//blank block<br />}<br />wordlength = j-i;</p><p>if (wordlength > 0)<br />{<br />memcpy(reversingText + textLength - j, originalText + i,wordlength);<br />wordCount++;<br />wordlength = 0;<br />}<br />else<br />{<br />//Blank block<br />}</p><p>if (j < textLength)<br />{<br />reversingText[textLength - j - 1] = originalText[j];<br />}<br />else<br />{<br />break;<br />}</p><p>i = j+1;<br />}</p><p>reversingText[textLength] = '/0';<br />memcpy(originalText, reversingText, textLength);<br />printf("逆序文本:/"%s/"/n",originalText);<br />return originalText;<br />}</p><p>int main()<br />{<br />char str[Max];<br />printf("請輸入文本:");<br />gets(str);</p><p> ReversedText(str);<br /> printf("/n");</p><p>system("pause");<br />return 0;<br />}

 

#include "stdio.h"<br />#include <iostream><br />using namespace std;<br />//原:"What is your name+"<br />//目:"name+ your is What"<br />int main(void)<br />{<br />char s[]="What is your name+" ;<br />char *a = NULL ;<br />int l = strlen(s) - 1 ;<br />while( l-- >= 0 )<br />{<br />if ( *(s+l) == ' ' || !l )<br />{<br />a = s + l + ( ( l ) ? ( 1 ) : ( 0 ) ) ;<br />while ( *a && *a != ' ' ) putchar( *a++ ) ;<br />putchar(' ') ;<br />}<br />}<br />printf("/n");<br />system("pause");<br />return 0 ;<br />}

以上程式碼片段在VS2008下測試通過.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.