面试题目: 输入字符串“I am a student”,要求输出字符串“student a am I”
#include <stdio.h>
void main(){
char src[] = "I am a stdutent";
char *temp_start = src;
char *temp_end = src;
while (*temp_end != '\0')
{
temp_end++;
}
temp_end--;
char temp;
while (temp_start < temp_end)
{
temp = *temp_start;
*temp_start = *temp_end;
*temp_end = temp;
temp_start++;
temp_end--;
}
//reverseal all
char *index = src;
printf("The src value is %s\n", src);
while (true)
{
temp_start = index;
while (*index != '\0' && *index != ' ')
{
++index;
}
temp_end = --index;
index++;
while (temp_start < temp_end)
{
temp = *temp_start;
*temp_start = *temp_end;
*temp_end = temp;
temp_start++;
temp_end--;
}
if (*index != '\0')
{
++index;
}
else
{
break;
}
}
printf("The src value is %s\n", src);
}
字符串反转与子串处理
本文介绍了一种使用C语言实现字符串整体及其内部子串反转的方法。通过两次反转操作达到最终效果:首先反转整个字符串,然后依次反转字符串内的每个单词。代码详细展示了这一过程,并通过具体示例说明了如何实现。
1593

被折叠的 条评论
为什么被折叠?



