华为最新笔试题——编程题及答案.doc_第1页
华为最新笔试题——编程题及答案.doc_第2页
华为最新笔试题——编程题及答案.doc_第3页
华为最新笔试题——编程题及答案.doc_第4页
华为最新笔试题——编程题及答案.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

问题:输入一个字符串,用指针求出字符串的长度。答案:#include int main() char str20, *p; int length=0; printf(“Please input a string: ”); gets(str); p=str; while(*p+) length+;printf(“The length of string is %dn”, length);return 0;问题:使用C语言实现字符串中子字符串的替换描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc, char* strFind, char* strReplace),strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,结果就变成了:ABCDEFGHIJKLMNOPQgggUVWXYZ答案一:#include #include void StrReplace(char* strSrc, char* strFind, char* strReplace);#define M 100;void main()char s=ABCDEFGHIJKLMNOPQRSTUVWXYZ;char s1=RST;char s2=ggg;StrReplace(s,s1,s2);printf(%sn,s);void StrReplace(char* strSrc, char* strFind, char* strReplace) int i=0; int j; int n=strlen(strSrc); int k=strlen(strFind); for(i=0;in;i+) if(*(strSrc+i)=*strFind) for(j=0;jk;j+) if(*(strSrc+i+j)=*(strFind+j) *(strSrc+i+j)=*(strReplace+j); else continue; 答案二:#include #define MAX 100StrReplace(char *s, char *s1, char *s2) char *p; for(; *s; s+) for(p = s1; *p & *p != *s; p+); if(*p) *s = *(p - s1 + s2); int main() char sMAX; /s是原字符串 char s1MAX, s2MAX; /s1是要替换的 /s2是替换字符串 puts(Please input the string for s:); scanf(%s, s); puts(Please input the string for s1:); scanf(%s, s1); puts(Please input the string for s2:); scanf(%s, s2); StrReplace(s, s1, s2); puts(The string of s after displace is:); printf(%sn, s); return 0;答案三:#include #include #include #define M 100void StrReplace(char* strSrc, char* strFind, char* strReplace);int main()char s=ABCDEFGHIJKLMNOPQRSTUVWXYZ;char s1=RST;char s2=gggg;StrReplace(s,s1,s2);printf(%sn,s);return 0;void StrReplace(char* strSrc, char* strFind, char* strReplace)while(*strSrc != 0)if(*strSrc = *strFind)if(strncmp(strSrc,strFind,strlen(strFind) = 0 )int i = strlen(strFind);int j = strlen(strReplace);printf(i = %d,j = %dn,i,j);char *q = strSrc + i;printf(*q = %sn,q);while(*strSrc+ = *strReplace+) != 0);printf(strSrc - 1 = %sn,strSrc - 1);printf(*q = %sn,q);while(*strSrc+ = *q+) != 0);elsestrSrc+;elsestrSrc+;问题:编写一个程序实现功能:将字符串”Computer Secience”赋给一个字符数组,然后从第一个字母开始间隔的输出该串,用指针完成。答案:#include #include int main()char str=”Computer Science”; int flag=1;char *p=str;while(*p) if ( flag ) printf(“%c”,*p); flag = (flag + 1) % 2; p+; printf(“n”); return 0;问题:使用C语言实现字符串中子字符串的替换描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc, char* strFind, char* strReplace),strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,结果就变成了:ABCDEFGHIJKLMNOPQgggUVWXYZ答案一:#include #include void StrReplace(char* strSrc, char* strFind, char* strReplace);#define M 100;void main()char s=ABCDEFGHIJKLMNOPQRSTUVWXYZ;char s1=RST;char s2=ggg;StrReplace(s,s1,s2);printf(%sn,s);void StrReplace(char* strSrc, char* strFind, char* strReplace) int i=0; int j; int n=strlen(strSrc); int k=strlen(strFind); for(i=0;in;i+) if(*(strSrc+i)=*strFind) for(j=0;jk;j+) if(*(strSrc+i+j)=*(strFind+j) *(strSrc+i+j)=*(strReplace+j); else continue; 答案二:#include #define MAX 100StrReplace(char *s, char *s1, char *s2) char *p; for(; *s; s+) for(p = s1; *p & *p != *s; p+); if(*p) *s = *(p - s1 + s2); int main() char sMAX; /s是原字符串 char s1MAX, s2MAX; /s1是要替换的 /s2是替换字符串 puts(Please input the string for s:); scanf(%s, s); puts(Please input the string for s1:); scanf(%s, s1); puts(Please input the string for s2:); scanf(%s, s2); StrReplace(s, s1, s2); puts(The string of s after displace is:); printf(%sn, s); return 0;答案三:#include #include #include #define M 100void StrReplace(char* strSrc, char* strFind, char* strReplace);int main()char s=ABCDEFGHIJKLMNOPQRSTUVWXYZ;char s1=RST;char s2=gggg;StrReplace(s,s1,s2);printf(%sn,s);return 0;void StrReplace(char* strSrc, char* strFind, char* strReplace)while(*strSrc != 0)if(*strSrc = *strFind)if(strncmp(strSrc,strFind,strlen(strFind) = 0 )int i = strlen(strFind);int j = strlen(strReplace);printf(i = %d,j = %dn,i,j);char *q = strSrc + i;printf(*q = %sn,q);while(*strSrc+ = *strReplace+) != 0);printf(strSrc - 1 = %sn,strSrc - 1);printf(*q = %sn,q);while(*strSrc+ = *q+) != 0);elsestrSrc+;elsestrSrc+;问题:编写一个程序实现功能:将两个字符串合并为一个字符串并且输出,用指针实现。 char str120=“Hello ”, str220=“World ”;答案:#include int main() char str120=“Hello ”, str220=“World ”; char *p=str1, *q=str2; while( *p ) p+; while( *q ) *p = *q; p+; q+; *p = 0; printf(“%sn”, str1); return 0;问题:以下函数的功能是用递归的方法计算x的n阶勒让德多项式的值。已有调用语句p(n,x);编写函数实现功能。递归公式如下:答案:#include float p(int x,int n)float t,t1,t2;if(n= =0) return 1;else if(n= =1) return x;else t1=(2*n-1)*x*p(x,(n-1);t2=(n-1)*p(x,(n-2);t=(t1-t2)/n;retur

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论