|
发表于 2024-12-30 21:37:42
|
显示全部楼层
虽然做出来了但实现顺序搞反了,哈哈,下面是我写的
#include<stdio.h>
#define MAX 1024
int main()
{
char original[MAX], ch, bv;
int i = 0, j = 0, k, m[MAX] = {0}, n = 1, v, temp = 0;
printf("请输入分割符:");
scanf("%c", &bv);
getchar();
printf("请输入一个英文句子:");
while(1){
ch = getchar();
if(ch == bv || ch == '\n'){
if(i != j){
j = i;
m[n++] = j;
}
if(ch == '\n'){
break;
}
continue;
}
original[i] = ch;
i++;
}
n--;
original[i] = '\0';
for(k = 0; k < n; k++){
temp = (m[k + 1] - m[k]) > temp ? (m[k + 1] - m[k]) : temp;
}
temp += 1;
printf("分割结果已存放到result[%d][%d]的二维数组中...\n", n, temp);
char result[n][temp];
k = 0;
for(i = 0; i < n; i++){
for(v = 0, j = m[k]; j < m[k + 1]; j++){
result[i][v] = original[j];
v++;
}
result[i][v] = '\0';
k++;
}
printf("现在依次打印每个单词:\n");
for(i = 0; i < n; i++){
printf("%s\n", &result[i][0]);
}
return 0;
}
|
|