小程序 (五) 字符串分离

来源:互联网 发布:淘宝能更改实名认证吗 编辑:程序博客网 时间:2024/05/03 20:43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr)
 /*【输入】  pInputStr:  输入字符串
 
         lInputLen:  输入字符串长度                   
 
【输出】  pOutputStr:  输出字符串,空间已经开辟好,与输入字符串等长;
 
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出
 
示例   
输入:"abc def gh i        d"
 
输出:"abc,def,gh,i,d," */



#include<stdio.h>#include<stdlib.h>void DivideString(char *pInputStr,char *pOutputStr){char * temp = pOutputStr;while( *pInputStr == ' ')   pInputStr++;//char * k = pInputStr;//printf("%s\n",k);//while(*k++) //k与pInputStr不同步while(*pInputStr){   if( *pInputStr != ' ')   {      *temp++ = *pInputStr++;        }   else {   while( *pInputStr == ' ' )         {  *temp = ',';             pInputStr++;  }   temp++;      }     //printf("%s\n",temp);}*temp = '\0';//*temp ;//if( *(temp-1) == ',')  //      *(temp-1) = ' ';//pOutputStr = temp;}int main(){ int len = 0;char * pInputStr = "    I'm  a      bloody man.   ";char * c = pInputStr; while(*c++){  len++;}char *pOutputStr = (char *) malloc(sizeof(char)*len);//pOutputStr = pInputStr;DivideString(pInputStr, pOutputStr);printf("%s\n",pInputStr);printf("%s\n",pOutputStr);delete[] pOutputStr;system("pause");return 0;}