BUPT-homework/pset13/1-5-GetString.c

48 lines
985 B
C
Raw Normal View History

2021-12-09 17:26:31 +08:00
#include <stdio.h>
int getString(char *source, char *strPtr[]);
int main() {
char str[100005];
char *strPtr[1005] = {0};
int i, num;
/* gets(str); */
fgets(str, 100005, stdin);
num = getString(str, strPtr);
for (i = 0; i < num; i++)
puts(strPtr[i]);
return 0;
}
/* 请在这里填写答案 */
int getString(char *source, char *strPtr[]) {
int count = 0;
int loc;
int j;
// Find the first ones.
for (int i = 0; source[i] != '\0'; i++) {
if (source[i] != ' ') {
strPtr[count++] = &source[i];
loc = i;
break;
}
}
// Start populating the array by finding the spaces.
for (int i = loc; source[i] != '\0'; i++) {
if (source[i] == ' ') {
// Find the next alpha.
for (j = i; j < 1005 + i && source[j] != '\0'; j++) {
if (source[j] != ' ' && source[j] != '\n') {
strPtr[count++] = &source[j];
break;
}
}
source[i] = '\0';
i = j;
}
}
return count;
}