p就像是
[["test"], ["lest"]]
它打印p [j]意味着它打印"测试"
char **p;
p = explode[i] = split(eachLineOfLsInArray[i]);
for(j=0;p[j];++j)
puts(p[j]);
但在尝试打印p [j] [0]时给出了段错误,意思是试图打印" t"
char **p;
p = explode[i] = split(eachLineOfLsInArray[i]);
for(j=0;p[j];++j)
puts(p[j][0]);
编辑:
继承了整个代码。 Lorem ipsum dolor坐下来,精致的adipistur elit。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char **split(const char *s);
int main (int argc, char *argv[]){
char virtualLs[100];
char eachLineOfLsInArray[100][100];
scanf("%[^\t]", virtualLs);
char *eachLineOfLs;
eachLineOfLs = strtok(virtualLs, "\n");
int loopCounterForStuffing;
loopCounterForStuffing = 0;
while (eachLineOfLs != NULL)
{
strcpy(eachLineOfLsInArray[loopCounterForStuffing], eachLineOfLs);
eachLineOfLs = strtok(NULL, "\n");
++loopCounterForStuffing;
}
char **explode[sizeof(eachLineOfLsInArray)/sizeof(*eachLineOfLsInArray)];
int i,j, n = sizeof(eachLineOfLsInArray)/sizeof(*eachLineOfLsInArray);
int maximum = 0;
int maximumindex = 0;
int maximumname = 0;
for(i=0;i < n; ++i){
char **p;
p = explode[i] = split(eachLineOfLsInArray[i]);
for(j=0;p[j];++j)
{
putchar(p[j][0]);
printf("%c", p[j][0]);
}
}
return 0;
}
static int wordCount(const char *s){
char prev = ' ';
int wc = 0;
while(*s){
if(isspace(prev) && !isspace(*s)){
++wc;
}
prev = *s++;
}
return wc;
}
char **split(const char *s){
int i, wc = wordCount(s);
char *word, **result = calloc(wc+1, sizeof(char*));
char *clone = strdup(s);//Note that you are copying a whole
for(word=strtok(clone, " \t\n"); word; word=strtok(NULL, " \t\n")){
result[i++] = word;//or strdup(word); and free(clone); before return
}
return result;
}
答案 0 :(得分:1)
此代码初始化i
中的split()
并将打印循环限制为已读取的数据(并包含一些调试I / O,主要用于显示调试的其他技术)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char **split(const char *s);
int main(void)
{
char virtualLs[100];
char eachLineOfLsInArray[100][100];
scanf("%[^\t]", virtualLs);
char *eachLineOfLs;
eachLineOfLs = strtok(virtualLs, "\n");
int loopCounterForStuffing;
loopCounterForStuffing = 0;
while (eachLineOfLs != NULL)
{
printf("each: <<%s>>\n", eachLineOfLs);
strcpy(eachLineOfLsInArray[loopCounterForStuffing], eachLineOfLs);
eachLineOfLs = strtok(NULL, "\n");
++loopCounterForStuffing;
}
printf("LoopC = %d\n", loopCounterForStuffing);
char **explode[sizeof(eachLineOfLsInArray) / sizeof(*eachLineOfLsInArray)];
int i, j, n = sizeof(eachLineOfLsInArray) / sizeof(*eachLineOfLsInArray);
printf("n = %d\n", n);
for (i = 0; i < loopCounterForStuffing; ++i)
{
char **p;
p = explode[i] = split(eachLineOfLsInArray[i]);
for (j = 0; p[j]; ++j)
{
putchar(p[j][0]);
printf("%c", p[j][0]);
}
putchar('\n');
}
return 0;
}
static int wordCount(const char *s)
{
char prev = ' ';
int wc = 0;
while (*s)
{
if (isspace(prev) && !isspace(*s))
{
++wc;
}
prev = *s++;
}
return wc;
}
char **split(const char *s)
{
int i = 0;
int wc = wordCount(s) + 1;
printf("Word count = %d\n", wc);
char *word;
char **result = calloc(wc + 1, sizeof(char *));
char *clone = strdup(s);// Note that you are copying a whole
for (word = strtok(clone, " \t\n"); word; word = strtok(NULL, " \t\n"))
{
printf("Word: <<%s>>\n", word);
result[i++] = word;// or strdup(word); and free(clone); before return
}
result[i] = 0;
return result;
}
示例输出:
$ ./split <<< "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
each: <<Lorem ipsum dolor sit amet, consectetur adipiscing elit.>>
LoopC = 1
n = 100
Word count = 9
Word: <<Lorem>>
Word: <<ipsum>>
Word: <<dolor>>
Word: <<sit>>
Word: <<amet,>>
Word: <<consectetur>>
Word: <<adipiscing>>
Word: <<elit.>>
LLiiddssaaccaaee
$
输出是每个单词的第一个字符,在结束的行上打印两次。