从字符串数组中删除第一个和最后一个字符

时间:2020-04-27 09:18:53

标签: c arrays string

您好,我试图找到一种方法来删除字符串数组的第一个和最后一个字符。

我对阵列的数据洞察力是这样的:

“ Time1”:

“ 3.0”

我正在尝试专门删除双引号和char: 我该怎么用?

char item[100]; 
   char  LastArray[1000];
   while (NewMixedArray[i] != NULL)
   {
    sscanf (NewMixedArray[i],"\"%99[^\"]", item);
    LastArray[i]=item;
    printf("%d %s\n",i,LastArray[i]);
    i++;
   }

   i=0;
   while (  LastArray[i] != NULL)
   {
    printf("DONE: %d %s\n",i,LastArray[i]);
    i++;
   }

在第二个printf上,我只会得到最后一个值。

https://i.imgur.com/H2OFz1Q.png

我发现我使用命令的问题

LastArray [i] = strdup(item);因为我的LastArray是一个指针数组。

1 个答案:

答案 0 :(得分:0)

#include <stdio.h>
#include <string.h>

int main() {
    char string[1000], result[1000];
    strcpy(string, "\"Time1\":");

    printf("\nInitial String : %s", string);
    int i;
    int c = 0;
    do {
        if (string[i] != '\"' & string[i] != ':') {
            result[c] = string[i];
            c++;
        }
        i++;
    } while (string[i] != '\0');

    printf("\nResult : %s\n", result);
    return 0;
}

结果

$ ./ss

Initial String : "Time1":
Result : Time1