#include <stdlib.h>
char s[] = "abc efg hij";
void deleteBlanks(char* s)
{
char* i = s;
char* j = s;
int isFirstBlank = 1;
while(*i != '\0')
{
if(*i != ' ')
{
*j++ = *i++;
isFirstBlank = 1;
}
else if(*i == ' ' && isFirstBlank)
{
*j++ = *i++;
isFirstBlank = 0;
}
else
{
i++;
}
}
*j = '\0';
}
void reverse(char* s, int start, int end)
{
while(start < end)
{
char temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
void reverseAll(char* s)
{
int start = 0;
int end = 0;
char* ss = s;
while(*s != '\0')
{
if(*s != ' ')
{
end++;
}
else
{
reverse(ss, start, end-1);
start = end + 1;
end++;
}
s++;
}
reverse(ss, start, end-1);
}
int main()
{
deleteBlanks(s);
reverseAll(s);
printf("%s ", s);
}
谷歌笔试题-去除连续空格并翻转字符串
最新推荐文章于 2024-04-14 13:45:30 发布