C中的动态变量

时间:2016-04-30 03:04:54

标签: c

我想在程序中使用双组动态变量。我发现了类似的问题Is it possible to use a for loop to change a variable name in C?

我可以从中作出类似的例子

for (str[run] = 0 ; str[run] < 5 ; str[run]++)

来自另一个问题,但它在我的例子中不起作用。

我有一个更高级的版本,因为我一次使用两个动态变量集。我知道我们可以使用逗号在 for 循环中放置尽可能多的变量,但即使有两个,它看起来也很长而且不好看。所以我试着以不同的方式进行。

我想要的是第一次使用 str1 chr1

str2 chr2 第二次等等。

这是我的代码:

int main () {

    int run;                            //loop counter

    char str1[20] = "abracadabra";      //string #1
    ...

    char chr1 = 'a';                    //character #1
    ...

    for (run; run <= 5; run++)          // runs 5 times
    {
        printf("The string %c will be removed from %c characters. \n", str[run], chr[run]);
        rmchr(str[run], chr[run]);
        printf("New modified string is: %c \n\n", str[run]);
    }
    ...
}

我知道我的代码是错的,但我知道我非常接近真相:-)如果它有用,我也包括

#include <string.h> 

在我的主要功能之前。

这是我的完整计划:

/*
* A simple program to remove certain characters from the given strings                              
*/

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

int main () {

    int run;                            //loop counter

    char str1[20] = "abracadabra";      //string #1
    char str2[20] = "abracadabra";      //string #2
    char str3[20] = "abracadabra";      //string #3
    char str4[20] = "aaaa";             //string #4
    char str5[20] = "aaaa";             //string #5

    char chr1 = 'a';                    //character #1
    char chr2 = 'b';                    //character #2
    char chr3 = 'n';                    //character #3
    char chr4 = 'a';                    //character #4
    char chr5 = 'n';                    //character #5

    //remove certain characters from array
    void rmchr(char str[], char ch);    //rmchr stands for 'remove character'

    for (run; run <= 5; run++)          // runs 5 times
    {
        printf("The string %c will be removed from %c characters. \n", str[run], chr[run]);
        rmchr(str[run], chr[run]);
        printf("New modified string is: %c \n\n", str[run]);
    }

    return 0;
}

void rmchr(char str[], char chr)
{
   int i, j = 0;                        //loop counters
   int length;                          //length of array

   length = strlen(str);

   for (i = 0; i < length; i++) {
      if (str[i] != chr) {
         chr = str[i];
         str[j] = chr;
         j++;
      }
   }
   str[j] = '\0';
}

程序的最终版本,它完美运行并得到我老师的批准:

/*
* A simple program to remove certain characters from the given strings                              
*/

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

int main () {

    // print array before and after editing array
    void print_rmchr (char str[], char ch); 

    int run;                            //loop counter

    char str1[20] = "abracadabra";      //string #1
    char str2[20] = "abracadabra";      //string #2
    char str3[20] = "abracadabra";      //string #3
    char str4[20] = "aaaa";             //string #4
    char str5[20] = "aaaa";             //string #5

    char chr1 = 'a';                    //character #1
    char chr2 = 'b';                    //character #2
    char chr3 = 'n';                    //character #3
    char chr4 = 'a';                    //character #4
    char chr5 = 'n';                    //character #5

    print_rmchr(str1, chr1);
    print_rmchr(str2, chr2);
    print_rmchr(str3, chr3);
    print_rmchr(str4, chr4);
    print_rmchr(str5, chr5);

    return 0;
}

//remove certain characters from array
void rmchr(char str[], char ch) {
   int i, j = 0;    //loop variable
   int size;        //lengh 
   char new_str[20];    //new array

   size = strlen(str);

   for (i = 0; i < size; i++) {
      if (str[i] != ch) {
         new_str[j] = str[i];
         j++;
      }
   }
   new_str[j] = '\0';

   strcpy(str, new_str);

}

// print array before and after editing array
void print_rmchr (char str[], char ch){

    //remove certain characters from array
    void rmchr(char str[], char ch);

    printf("The string '%s' will be removed from '%c' characters. \n\n", str, ch);

    rmchr(str, ch);

    printf("New modified string is: '%s'. \n\n", str);
}

    /* In case you will need user input:
    //USER INPUT
    printf("Enter the string : \n");
    gets(str);

    printf("Enter character which you want to delete : \n");
    scanf("%ch", &ch);

    print_rmchr(str, ch);
    */

2 个答案:

答案 0 :(得分:1)

你只是要一个阵列吗? C可以很好地处理数组数组:

char str[5][20] = { 
    "abracadabra",      //string #0
    "another string",   //string #1
    ...
};

char chr[5] = { 'a', 'b', 'c', 'd', 'e' };  // 5 chars, #0 throuh #4

答案 1 :(得分:-1)

你需要的是这样的东西:

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

#define MAX_COUNT 5

int main () {

        int i;
        int run = 0;                            //loop counter
        char *str[MAX_COUNT];
        char str_const[MAX_COUNT][20] = {
                "abracadabra", "something", "more", "new", "old",
        };
        const char chr[MAX_COUNT] = {'a', 'e', 'r', 'e', 'd'};

        for (i = 0; i < MAX_COUNT; i++) {
                str[i] = malloc(strlen(str_const[i]) + 1);
                strcpy(str[i], str_const[i]);
        }

        for (run = 0; run < MAX_COUNT; run++)  {         // runs 5 times
                printf("The string %s will be removed from %c characters. \n", str[run], chr[run]);
                rmchr(str[run], chr[run]);
                printf("New modified string is: %s \n\n", str[run]);
        }

 return 0;

所以这是解释: 你需要一个基本上是你需要的字符数组的chr数组。但是,str需要是一个指向字符的指针数组。因此str数组只保存字符串的地址,然后您可以使用该地址来访问字符串。希望有所帮助。