[英]Array of chars from an array of pointer to strings in c
我有一個來自用戶的行輸入,我存儲在一個字符input
數組中。 我將字符串input
復制到另一個數組words
並使用帶有空格分隔符的strtok
將單個單詞存儲到指向字符串astArray[]
的指針數組中。 現在單個單詞存儲在*astArray
指向的內存位置和*token
指向的單詞的單個字符中。
在修改了需要對單詞執行的操作后,我想用存儲在astArray
的字符串重新構建 char input
數組。 我想將所有字符串復制到字符input
數組中,它們之間有一個空格,最后是一個空值。 我該怎么做? 我試過strcpy
但我得到的字符之間沒有空格。
內存已分配。
* token
token = strtok(words, " ");
loop
strcpy(astArray[i], token);
char *astArray[] = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
char input[128];
int wc = 9;
char *out = input;
int i;
for(i=0;i<wc;++i){
char *p = astArray[i];
while(*p){
*out++=*p++;
}
if(i<wc-1){
*out++ = ' ';
}
}
*out = '\0';
printf("%s\n", input);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.