|
Post by huangno1 on Feb 28, 2017 20:57:02 GMT -8
[ Join and Split (105-1 Final) ]
#include <stdio.h>
#include <string.h>
constexpr int MaxWordLen = 21;
int split(char words[][21],char sentence[], char delim)
{
int i=0,j=0,k=0;
while ( sentence[ i ] != '\0' ){
if (sentence [ i ] != delim ){
words [ j ] [ k ] = sentence [ i ] ;
i++;k++;
} else{
words [ j ][ k ] = '\0' ;
i++;j++;k=0;
}
}
return j+1;
}
// -----------------------------------------------------------------------
void join(char sentence[],char words[][21],int size, char delim)
{
int j=0;
for (int i=0; i<size; i+=1){
int k=0;
while(words[k]!='\0'){
sentence[j]=words[k];
k++;
j++;
}
if(i<size-1)
sentence[j]=delim;
j++;
}
sentence[j]='\0';
}
// -----------------------------------------------------------------------
void print(char word[][21],int size)
{
for (int i=0; i<size; i+=1)
{
printf("[%s]\n", word);
}
}
// -----------------------------------------------------------------------
int main()
{
char words[10][MaxWordLen]= {}, sentence[250]= {};
gets(sentence);
char delim;
scanf(" %c", &delim);
int s = split(words, sentence, delim);
print(words, s);
scanf(" %c", &delim);
join(sentence, words, s, delim);
printf("%s\n", sentence);
return 0;
}
|
|