當前位置:編程學習大全網 - 編程語言 - 設計回文判斷程序,對輸入的字符串進行回文判斷

設計回文判斷程序,對輸入的字符串進行回文判斷

“構造棧;構造隊列; 使用棧

和隊列進行判斷”這個我也不是太懂。

我在網上看到過回文,使用了好多復雜的函數,我都還沒學過。

不過我自己寫了壹個,妳看看,原理很簡單。

用了.substr函數 for循環 getlin(提取壹句話,遇回車終止)

是在C++裏寫的,不知道在C裏能不能用。

<這裏在C++的getline(cin,stringname函數有個bug(第壹次從鍵盤getlin輸入兩個回車才能getlin,但是第二個回車會保存到下壹次的getlin)所以循環getline時會出錯> 我把循環去掉了。

//*************************************************************

//Palindrome Judgment program 回文判斷程序 U6ex4 page235

//cin a sentence,cout its characters in reverse order

//and judgment whether the input line is a palindrome

//*************************************************************

#include<iostream>

#include<string>

using namespace std;

string sen;

string pal;//palindrome回文

int len,i,j;//len=length字符串的長度,i是for循環變量,j判斷倒序與正序相同字符的個數

//如果它等於字符串總的長度,即為回文。

void main()

{

cout<<"please input a sentence\

";

getline(cin,sen);

cout<<"the reverse order is:\

";

len=sen.length();

j=0; //倒序與正序相同字符的個數,賦初值0

for(i=0;i<len;i++)

{

pal=sen.substr(len-i-1,1);

cout<<pal;

if(pal==sen.substr(i,1))

j++;

}

if(j==len)

cout<<endl<<"this sentence IS a palindrome\

";

else

cout<<endl<<"this sentence is NOT a palindrome\

";

}

  • 上一篇:怎樣才能叫做電腦高手啊
  • 下一篇:B站追番人數超過400萬的九部動畫分別是什麽,妳都看過了嗎?
  • copyright 2024編程學習大全網