C++實驗十四_指針與數(shù)組_第1頁
C++實驗十四_指針與數(shù)組_第2頁
C++實驗十四_指針與數(shù)組_第3頁
C++實驗十四_指針與數(shù)組_第4頁
C++實驗十四_指針與數(shù)組_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、C+實驗十四 指針與數(shù)組1范例:判斷用戶輸入的C字符串是否為"回文",所謂"回文"是指順讀和反讀都一樣的串, 例如串 12321、madam?!境绦颉?#include<iostream>#include<cstring>using namespace std;const int SIZE=100;int main()char carraySIZE;int i,len,is_palindrome=1;cout<<"Please input a string:"<<endl;cin.get

2、(carray,SIZE);len=strlen(carray);for(i=0;i<len/2;i+)if(carrayi!=carraylen-1-i)is_palindrome=0;break;if(is_palindrome) cout<<"The string is a palindrome"<<endl;else cout<<"The string isn't a palindrome"<<endl; return 0;2. 【要求】(1) 重新定義回文為:濾去所有非字母

3、字符(包括空格)后,不考慮字母的大小寫,從左向右和從右向左讀都相同的詞或短語。如,”Madam, Im adam” 和 “Golf ,No Sir ,prefer prison flog!”#include<iostream>#include<cstring>using namespace std;int main()char carray10;int i,len,is_palindrome=1,a,b=0,j;cout<<"Please input a string:"<<endl;cin.get(carray,10);fo

4、r(a=0;a<10;a+)if(carraya!=' ' && carraya!='!' && carraya!='?' && carraya!=',' && carraya!='.')carrayb=carraya;else continue;b+;carrayb='0'len=strlen(carray);for(j=0;j<len;j+)if(carrayj>=65&&carrayj<=9

5、0) carrayj=carrayj+32;for(i=0;i<len/2;i+)if(carrayi!=carraylen-1-i)is_palindrome=0;break;if(is_palindrome) cout<<"The string is a palindrome"<<endl;else cout<<"The string isn't a palindrome"<<endl;return 0;(2) 編寫一個判斷輸入字符串是否為回文的函數(shù),并使用指針形式訪問數(shù)組元素。#incl

6、ude<iostream>#include<cstring>using namespace std;const int SIZE=100;int main()char carraySIZE,*p;int i,len,is_palindrome=1;cout<<"Please input a string:"<<endl;p=carray;cin.get(carray,SIZE);len=strlen(carray);for(i=0;i<len/2;i+)if(pi!=plen-1-i)is_palindrome=0;br

7、eak;if(is_palindrome) cout<<"The string is a palindrome"<<endl;else cout<<"The string isn't a palindrome"<<endl;return 0;2. 范例:按一定的規(guī)則可以將一個字符串經(jīng)加密轉(zhuǎn)換為一個新的串,例如加密的簡單方法是當為'a''y'的小寫字母時用后一個字母代替前一個字母,其中'z'變換為'a',其他字符時不變。例如: 原串為 T

8、his is a secret code! 加密后的串為 Tijt jt b tfdsfu dpef! 編寫一個程序?qū)斎氪用?,輸出加密前和加密后的串,再將加密后的字符串解密輸出。主函?shù)如下,#include<iostream>#include<cstring>using namespace std;void secret(char st);void desecret(char st);int main()char st="This is a secret code!"cout<<st<<endl;secret(st);co

9、ut<<st<<endl;desecret(st);cout<<st<<endl;return 0;void secret(char st)int a,b;char *p;p=st; a=strlen(st);for(b=1;b<a-1;b+)if(pb=122)pb=97;continue;if (pb=' '| pb='!' | pb='?' | pb=',' | pb='.')continue;pb=pb+1;void desecret(char st)i

10、nt a,b;char *p;p=st;a=strlen(st);for(b=1;b<a;b+)if(pb=97)pb=122;continue;if (pb=' '| pb='!' | pb='?' | pb=',' | pb='.')continue;pb=pb-1; (1) 閱讀程序,如果將兩個函數(shù)中else if(*s=122) *s='a'和else if(*s=97) *s='z'處的else 去掉,對程序有何影響?使用數(shù)據(jù)”I am a boy !” 重新測試看看

11、。答:結(jié)果還是I am a boy !,但是如果輸入數(shù)據(jù)里面含有z,就會輸出。(2) 仿造上例編寫程序:設(shè)計一個帶密鑰的加密算法,例如密鑰可以是一個常數(shù),字符串加密的方法是將每個字符的ASCII碼值加上該常數(shù),然后對128求模。要求以密鑰將加密的字符串加密輸出,再以相同的密鑰將加密字符串解密輸出。 #include<iostream>#include<cstring>using namespace std;void secret(char st);void desecret(char st);const m=10;int main()char st="

12、;This is a secret code!"cout<<st<<endl;secret(st);cout<<st<<endl;desecret(st);cout<<st<<endl;return 0;void secret(char st)int a,b;char *p;p=st; a=strlen(st);for(b=1;b<a-1;b+)if(pb=122)pb=97;continue;if (pb=' '| pb='!' | pb='?' | pb=

13、',' | pb='.')continue;pb=pb+m; pb=pb%128;void desecret(char st)int a,b;char *p;p=st;a=strlen(st);for(b=1;b<a;b+)if(pb=97)pb=122;continue;if (pb=' '| pb='!' | pb='?' | pb=',' | pb='.')continue;pb=pb-m;if(pb<97) pb=pb+128-m; 3編程:重新編寫實驗十三中題3

14、的字符串處理函數(shù),用指針作為參數(shù)。 #include<iostream>#include<cstring>using namespace std;void trim (char *s);void leftstring (char *s1, char *s2, int n);int index( char *s1,char *s2);int main()char str1="Im student. ",str2="student",str34;int n;cout<<"包含尾部空格的串str1:"&l

15、t;<str1<<"長度為:"<<strlen(str1)<<endl;trim(str1);cout<<"無尾部空格的串str1:"<<str1<<"長度為:"<<strlen(str1)<<endl;leftstring(str1,str3,3);cout<<"串str3:"<<str3<<"長度為:"<<strlen(str3)<<

16、;endl;cout<<"串str2:"<<str2<<endl; n=index(str1,str2);if(n!=-1) cout<<"串str1包含子串str2,從第"<<n<<"字符開始(有0開始計數(shù))."<<endl; else cout<<"串str1不包含子串str2"<<endl;return 0;void trim (char *s)int i;for(i=0;i<20;i+)if(si=' '&&si+1=' ') si='0'break;void le

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論