第一次用vc做面试题,原题如下:
文件评审
Michel需要评审一份文件,但是却有另一件紧急的事情要处理,于是请好友Jack帮忙评审一下。当Michel看到Jack评审结果后,发现一个问题,Jack是以第一人称评审的,现有Michel需要将Jack评审中第一人称(we)和第二人称(you)进行置换(you换成we,we换成you),并要求在句首的单词首字母大写。现在需要请你帮忙完成这个工作。
Jack的评审文字单词之间是以空格” ”或者标准标点符号”,” “.” “?” “!” “;”作为分隔符的,评审文字内容不超过1024个字符长度。
假设Michel需要的评审文字如下:
sample_input:You are students,you should not get the weapon.
sample_output: We are students,we should not get the weapon.
sample_input: You are students,you should not get the weapon.
sample_output: we are students,we should not get the weapon.
sample_input: You are students, you should not get the weapon.
sample_output: We are the students, we should not get the weapon.
代码输入部分参考:
#define MAX_SIZE (1024)
int main(void)
{
int ncmp = 0;
char szInput[MAX_SZIE * 2] ;
memset(szInput, 0, sizeof(char) * MAX_SIZE * 2);
//! 处理输入【不能超过MAX_SIZE个字符,否则报错】
gets(szInput);
if (strlen(szInput) >= MAX_SIZE)
{
printf(“Err:Out of range!”);
return -1;
}
//! 添加自己的业务处理函数
return 0;
}
测试用例参考:
用例1:
输入:You are students,you should not get the weapon.
输出:We are students,we should not get the weapon.
------------------------------------------------------------------------------------------------------------
由于对VC不是很熟悉,所以写出了效率很低的代码(用了大量的strlen,strcpy等函数),但是功能都实现了。
主要思想是:如果一个we的前面有分隔符,后面也有分隔符,就把它当作是一个需要替换的字符串,对于句首we的识别,我用了比较偷懒的方法,再句首插入一个
分割符,这样就不用判断句首了,最后再将句首字母大写。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE (1024)
//大写字符转小写字母
char upper_to_lower(char chSymbol)
{
if (('A' <= chSymbol) && ('Z' >= chSymbol))
{
return chSymbol + 32;
}
else
{
return chSymbol;
}
}
//小写字符转大写字母
char lower_to_upper(char chSymbol)
{
if (('a' <= chSymbol) && ('z' >= chSymbol))
{
return chSymbol - 32;
}
else
{
return chSymbol;
}
}
//input: char *pszInput, int nInputLen, int nInputSize
//output: char *pszInput
int review(char *pszInput, int nInputLen, int nInputSize)
{
int i = 0;
int j = 0;
char szTemp[MAX_SIZE * 2] = "";
memset(szTemp, 0x0, MAX_SIZE * 2);
szTemp[0] = '.';
nInputLen = nInputLen + 1; //增加了空格符。
strcpy(szTemp + 1, pszInput);
//printf("给临时字符串加了空格:<%s>\n", szTemp);
//将源字符串的开头增加了一个'.'。
for(j = 0; j < nInputLen; j++)
{
*(pszInput + j) = *(szTemp + j); //将头部增加了'.'字符的临时字符串赋给源字符串。
}
pszInput[j] = '\0';
//printf("给输入字符串加了空格:<%s>\n", pszInput);
for (i = 0; i < nInputLen; i++)
{
//将we替换为you
if ((*(pszInput + i + 0) == ' ') //带替换字符的前后都有分隔符。
|| (*(pszInput + i + 0) == ',')
|| (*(pszInput + i + 0) == '.')
|| (*(pszInput + i + 0) == '?')
|| (*(pszInput + i + 0) == '!')
|| (*(pszInput + i + 0) == ';')
)
{
if ((upper_to_lower(*(pszInput + i + 1)) == 'w') //使用upper_to_lower,防止出现We时不能兼容
&& (upper_to_lower(*(pszInput + i + 2)) == 'e')
)
{
if ((*(pszInput + i + 3) == ' ')
|| (*(pszInput + i + 3) == ',')
|| (*(pszInput + i + 3) == '.')
|| (*(pszInput + i + 3) == '?')
|| (*(pszInput + i + 3) == '!')
|| (*(pszInput + i + 3) == ';')
)
{
*(pszInput + i + 1) = 'y'; //如果是We的话,此时应该为You才对
*(pszInput + i + 2) = 'o';
memset(szTemp, 0x0, MAX_SIZE * 2);
strcpy(szTemp, pszInput + i + 3);
//printf("we后的字符串右移后:<%s>\n", szTemp);
*(pszInput + i + 3) = 'u';
nInputLen = nInputLen + 1; //增加了一个'u',源字符串变长。
if (nInputSize < nInputLen)
{
return -1; //越界
}
strcpy(pszInput + i + 4, szTemp);
i = i + 3; //已经处理了3个字符,所以向后向后偏移3.
}
}
}
//将you替换为we
if ((*(pszInput + i + 0) == ' ')
|| (*(pszInput + i + 0) == ',')
|| (*(pszInput + i + 0) == '.')
|| (*(pszInput + i + 0) == '?')
|| (*(pszInput + i + 0) == '!')
|| (*(pszInput + i + 0) == ';')
)
{
if ((upper_to_lower(*(pszInput + i + 1)) == 'y')
&& (upper_to_lower(*(pszInput + i + 2)) == 'o')
&& (upper_to_lower(*(pszInput + i + 3)) == 'u')
)
{
if ((*(pszInput + i + 4) == ' ')
|| (*(pszInput + i + 4) == ',')
|| (*(pszInput + i + 4) == '.')
|| (*(pszInput + i + 4) == '?')
|| (*(pszInput + i + 4) == '!')
|| (*(pszInput + i + 4) == ';')
)
{
*(pszInput + i + 1) = 'w';
*(pszInput + i + 2) = 'e';
for (j = i + 3; j < nInputLen; j++)
{
*(pszInput + j) = *(pszInput + j + 1); //从i + 3开始,所有字符串左移动。
}
*(pszInput + nInputLen - 1) = '\0'; //因为you==》we少了一个字符,所以\0也应该向前移动一位。
nInputLen = nInputLen - 1;
i = i + 2; //已经处理了2个字符,所以向后向后偏移2.
}
}
}
}
//去掉头部'.'
memset(szTemp, 0x0, MAX_SIZE * 2);
strcpy(szTemp, pszInput + 1);
strcpy(pszInput, szTemp);
//确保字符串的开头为大写
*pszInput = lower_to_upper(*pszInput);
return 0;
}
int main(void)
{
int nRes = -1;
char szInput[] = "We we you you students wewe, you you youyou, in the world.";
int nInputLen = strlen(szInput);
int nInputSize = MAX_SIZE;
printf("Input string is:<%s>\n", szInput);
nRes = review(szInput, nInputLen, nInputSize);
if (0 != nRes)
{
printf("Err: review fault!");
}
printf("Output string is:<%s>\n", szInput);
return 0;
}