经典的2个C语言面试题目

来源:互联网 发布:人工智能技术发展方向 编辑:程序博客网 时间:2024/04/30 17:14

以前面试遇到的,好像很多面试的人都遇到过这些题目,现在贴出来,并写上答案,找工作的朋友可以参考参考。

1.将字符串“ABCD1234abcd”倒转。

#include <stdio.h>
#include <string.h>
#include <dos.h>
int main()
{
char str[] = "ABCD1234abcd";
int length = strlen(str);
char * p1 = str;
char * p2 = str + length - 1;
while(p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
}
printf("str now is %s/n",str);
system("pause");
return 0;
}

2.从键盘中输入一个字符串,将小写字母全部转化成大写字母,然后输出到一个磁盘文件test中保存,输出字符串以'!'结束。

#include "stdio.h"
main()
{FILE *fp;
char str[100],filename[10];
int i=0;
if((fp=fopen("test","w"))==NULL)
{ printf("cannot open the file/n");
exit(0);}
printf("please input a string:/n");
gets(str);
while(str[i]!='!')
{ if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
fputc(str[i],fp);
i++;}
fclose(fp);
}