恒和大风 - 机试题

来源:互联网 发布:mac装linux虚拟机 编辑:程序博客网 时间:2024/04/29 23:31

1. 不用string.h实现一句话的逆序输出

char c[] = "abcdefg";
int c_length = sizeof(c)-1;

char* pszOut = (char*)malloc(c_length+1);
char* temp_pszOut = pszOut;

while (c_length-->0)
{
*temp_pszOut++ = *(c + c_length);
}

*temp_pszOut = '\0';

printf("%s\n",pszOut);

注:sizeof在其中的应用,如果是对char*,则始终为4,最初定义char[256],则长度定为256

2. 实现“This is a dog”的输出,输出为"dog a is This"

       char c[] = "This is a dog";
int c_length = sizeof(c);

char* pszOut = (char*)malloc(c_length);

char* s[5];
int s_length = 0;

s[0]=pszOut;

for (int i=0;i<c_length;i++)
{
*pszOut++ = *(c+i);
if (*(c+i)==' ')
{
*pszOut++ = '\0';
s[++s_length]=pszOut;
}
}

for (int j=s_length;j>=0;j--)
{
printf("%s",s[j]);
}

3. string.cpp的实现,查看我的收藏

原创粉丝点击