0

I want to use double set of dynamic variables in my program. I have found similar issue, Is it possible to use a for loop to change a variable name in C?

I can make similar example from it as

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

from the other question, but it will not work in my example.

I have a little bit more advanced version, since I use two dynamic sets of variables at once. I know we can put as many variables as we can in the for loop, using a comma, but even with two it would look really long and not good-looking. So I tried to go in the different way.

What I want is to use str1 and chr1 first time.

str2 and chr2 second time ans so on.

Here is my code:

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]);
    }
    ...
}

I know my code is wrong, but I know I am very close to the truth :-) If it would be useful, I also included

#include <string.h> 

before my main function.

Here is my full program:

/*
* 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';
}

FINAL version of a program, it runs perfectly and got approved by my teacher:

/*
* 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);
    */
Community
  • 1
  • 1
  • 2
    Are you sure `run` is initialized in your `...` part? – MikeCAT Apr 30 '16 at 03:08
  • "What I want is to use str1 and chr1 first time. str2 and chr2 second time ans so on." I guess you cannot do this because typical systems won't hold variable names in executable file except for in information for debugging. Why not use arrays? – MikeCAT Apr 30 '16 at 03:09
  • MikeCAT, I have to use strings, that is part of my homework. In my ... part is just from str1 till str 5 and from chr1 till chr5 and etc. Nothing special. –  Apr 30 '16 at 03:10
  • Why not an array of pointers to the strings? – Rivasa Apr 30 '16 at 03:12
  • 1
    Using strings won't conflict with using arrays. Or does "strings" have some special meanings in your class? – MikeCAT Apr 30 '16 at 03:12
  • I could use arrays, indeed, but on my assignment the 5 sets of given characters are labeled as "strings". –  Apr 30 '16 at 03:27

2 Answers2

1

Are you just asking for an array? C can handle arrays of arrays just fine:

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

char chr[5] = { 'a', 'b', 'c', 'd', 'e' };  // 5 chars, #0 throuh #4
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • I am not sure what you mean here, but it looks useful. I have posted my full version in the end of the question to make more sense of what I want. –  Apr 30 '16 at 03:23
-1

What you need is something like this:

#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;

So here is the explanation: You need a chr array that is basically an array of characters that you need. However, str needs to be an array of pointers to characters. So str array will just hold the address of the string and then you can use that address to access the string as you like. Hope that helps.

Furquan
  • 676
  • 3
  • 5
  • This program seems invalid. String literals cannot be modifed. – MikeCAT Apr 30 '16 at 03:17
  • When I edit my code, using your modifications, the compiler lets me run, but brakes soon after - that's much better then my earlier version (my version doesn't even let me compile) :-). But still something doesn't look right. –  Apr 30 '16 at 03:20
  • @MikeCAT: That is correct. I did not read carefully the function. It seems to be removing the character. – Furquan Apr 30 '16 at 04:16
  • @Mikhail: Yes, it will crash because you would be trying to edit string literals. What you would need is allocating memory for it and then edit them. Let me correct the solution. – Furquan Apr 30 '16 at 04:17