给一个字符串,有大小写字母,要求写一个函数把小写字母放在前面 大写字母放在后面,尽量使用最小空间,时间复杂度。(即用指针做)。 如:aAbBcCdD ---àabcdABCD

来源:互联网 发布:乐视视频网络连接 编辑:程序博客网 时间:2024/06/07 21:33

给一个字符串,有大小写字母,要求写一个函数把小写字母放在前面

大写字母放在后面,尽量使用最小空间,时间复杂度。(即用指针做)。

如:aAbBcCdD ---àabcdABCD


#include <stdio.h>

#include <stdlib.h>

int SmallToCaptial( char *str, char *outbuf )
{
char *p = str;

if (str == NULL || outbuf == NULL)
{
return -1;
}

while (*p)
{
if (*p >= 'a' && *p <= 'z')
{
*outbuf++ = *p; //小写字母放在前面
}
p++;
}
p = str;

while (*p)
{
if (*p >= 'A' && *p <= 'Z')
{
*outbuf++ = *p; //找到大写字母接在后面
}
p++;
}
*outbuf = '\0'; //加上结束标志

return 0;
}

int main()
{
char *str = NULL; //含有大小写字母的字符串
char outbuf[100] = {0}; //存放处理好的字符串
str = (char *)malloc(100 * sizeof(char));

printf ("please input a string (with capital letter and small letter):\n");
scanf ("%s", str);
printf ("The original string is %s\n", str);

if( SmallToCaptial(str, outbuf) == -1 )
{
printf ("function SmallToCaptial error!\n");
return -1;
}

printf ("The result is %s\n", outbuf);

free(str);

return 0;
}
阅读全文
0 0