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

xiaoxiao2021-02-28  71

/**************************************************************************  * 将给定的含有大小写字母的字符串中的小写字母放在前面,大写字母放在后面  * 如 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; }
转载请注明原文地址: https://www.6miu.com/read-37187.html

最新回复(0)