2012华为校招上机一道----字符串分离

题目描述:
通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空

格分割。请编写一个程序,自动分离出各个子串,并使用’,’将其分离,并

且在最后也补充一个’,’,并将子串存储。

要求实现函数:

void DivideString (const char *pInputStr, long IInputLen, char

*pOutputStr );

【输入】   pInputStr:       输入字符串
                   IInputLen:       输入字符串长度
【输出】   pOutputStr:     输出字符串,空间已经开劈好,与输入输

入字符串等长;
【注意】   只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例:
输入:”abc def ghi       d”
输出:”abc,def,ghi,d”

***************************************************************
【分析】
典型的字符串被分割问题
首先要想到空格字符可能出现的三种位置:串前,串中,串尾,针对这三

种不同的位置有没有同样的算法?
其次要想到分割的空格是单独出现,还是连续出现?

想明白上面两个问题以后,可以敲代码予以实现。

一、C Code:

//============================================================================
// Author      : liuchundong
// Version     : 0.1
// Copyright   : Your copyright notice
//============================================================================
#include<stdio.h>
#include<conio.h>

void DivideString(char *pInputStr,char *pOutputStr)

{

             char *tempChar;                                                   //临时指针指向输出首地址
             tempChar = pOutputStr;
 
             while(*pInputStr == ‘ ‘)                                          //去掉字符串前的若干空格
             {
                       pInputStr++;
              }

             while(*pInputStr)                                                  //第一个不为空格的字符开始循环
             {
                       if(*pInputStr != ‘ ‘)                                        //不为空格则将该字符复制到pOutputStr里面
                       {
                                  *tempChar++ = *pInputStr++;
                       }
                       else                                                               //碰到一个或若干个空格连续着,则只在pOutputStr里面加一个’,’,其余忽略继续往下找
                      {
                                  while(*pInputStr == ‘ ‘) //跟最开始去掉字符串前面的空格同理
                                  {
                                             *tempChar = ‘,’;
                                             pInputStr++;
                                  }
                                  tempChar++;
                       }
            }
           *tempChar=’\0′;      //结束符号
           if(*(tempChar-1)!=’,’)    //判断最后一个是不是’,’
           {
                     *tempChar = ‘,’;
                     *(tempChar+1) = ‘\0’;
           }
}

int main()
{

           char *c, *pIn =”  Janpanese is a big    shit       “;
           long len=0;
           c = pIn;
           while(*c++)
           {
                    len++;
           }
           char pOut[len];

           DivideString(pIn,pOut);
 
           printf(“%s\n”,pIn);
           printf(“%s”,pOut);
 
           getch();
           return 0;
}

 

二、JAVA Code:

 1、思想跟前面C Code一样:

public class DivideString {
          static String result1;
          public static void divideStringByCut(String inputStr,String outputStr){
                    int i = 0;
                    String tmpStr = “”;
       

                    while(inputStr.charAt(i) == ‘ ‘){   //省略掉前面的空格
                                i++;
                     }

                    while(i < inputStr.length()){    //从第一个不为空格的字符起,开始复制
                                if(inputStr.charAt(i) != ‘ ‘){
                                            outputStr += inputStr.charAt(i);
                                            i++;
                                }else{
                                            while(i < inputStr.length() && inputStr.charAt(i) == ‘ ‘){ //省略掉字符串中间的空格,只在空格完了后添加一个逗号
                                            tmpStr = “,”;
                                            i++;
                                 } 
                                 outputStr += tmpStr;
                     }
           }
           //考虑首末尾字符不是逗号的情况
           if(outputStr.charAt(outputStr.length()-1) != ‘,’){
                     outputStr+=”,”;
           }
           result1 = outputStr;
           /*测试*/           
           public static void main(String[] args) {

                      String pInStr = ”   Japanese    is a SBBB    “;
                      String pOutStr = “”;
                      DivideString.divideStringByCut(pInStr, pOutStr);

                      System.out.println(pInStr);
                      System.out.println(result1);
           }

}

2、下面这个方法简直就是神了,但对于java比较熟,对正则比较熟悉的人来说,就太easy啦!写出来都不敢相信。瞬间有种残废的感觉!!!想自宫~~~~~~~~~~~

public class DivideString {
          static String result2;

          public static void divideStringBySplit(String inputStr,String outputStr){
                     inputStr = inputStr.trim();                                       //考虑首末尾字符不是逗号的情况
                     outputStr = inputStr.replaceAll(“\\s+”,”,”);            //将一个或多个空格替换为一个逗号       

                     if(outputStr.charAt(outputStr.length()-1) != ‘,’){   //考虑首末尾字符不是逗号的情况
                               outputStr+=”,”;
                     }
                    result2 = outputStr;
            }

            /*测试*/   

            public static void main(String[] args) {

                      String pInStr = ”   Japanese    is a SBBB    “;
                      String pOutStr = “”;
                      DivideString.divideStringByCut(pInStr, pOutStr);

                      System.out.println(pInStr);
                      System.out.println(result2);
           }

}

点赞