C++实验十四_指针与数组_第1页
C++实验十四_指针与数组_第2页
C++实验十四_指针与数组_第3页
C++实验十四_指针与数组_第4页
C++实验十四_指针与数组_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+实验十四 指针与数组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) 编写一个判断输入字符串是否为回文的函数,并使用指针形式访问数组元素。#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. 范例:按一定的规则可以将一个字符串经加密转换为一个新的串,例如加密的简单方法是当为'a''y'的小写字母时用后一个字母代替前一个字母,其中'z'变换为'a',其他字符时不变。例如: 原串为 T

8、his is a secret code! 加密后的串为 Tijt jt b tfdsfu dpef! 编写一个程序对输入串加密,输出加密前和加密后的串,再将加密后的字符串解密输出。主函数如下,#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) 阅读程序,如果将两个函数中else if(*s=122) *s='a'和else if(*s=97) *s='z'处的else 去掉,对程序有何影响?使用数据”I am a boy !” 重新测试看看

11、。答:结果还是I am a boy !,但是如果输入数据里面含有z,就会输出。(2) 仿造上例编写程序:设计一个带密钥的加密算法,例如密钥可以是一个常数,字符串加密的方法是将每个字符的ASCII码值加上该常数,然后对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、的字符串处理函数,用指针作为参数。 #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开始计数)."<<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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论