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);
*/