0

I'm trying to modify a previous program I wrote using pure pointer notation. It's a program that generates a random string of 40 uppercase letter, takes input of up to 20 uppercase letter, and input of a character. The program replaces reoccurring characters in the generated string with the character entered. Right now, I'm trying to pass the parameters of the randomly generated string so I can access them in the second function and can't figure out how to do it.

Thank you for the help.

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

/* Function prototypes */
void fillS1(char * x);

void fillS2(char * x, char * y, char z);

void strFilter(char * a, char * b, char c);

int main(int argc, const char * argv[])
{

char s1[41];
char s2[21];
char x = 0;

char * pointerToS1;
char * pointerToS2;

pointerToS1 = s1;
pointerToS2 = s2;

fillS2(pointerToS2, pointerToS1, x);

return 0;
}

/* Function to generate a random string of 40 uppercase letters */
void fillS1(char * randomlyGeneratedPointer)
{
char randomlyGeneratedArray[41];

randomlyGeneratedPointer = randomlyGeneratedArray;

int i;

for (i = 0; i < 40; i++) {
    *(randomlyGeneratedPointer + i) = 'A' + rand() % 26;
}


}

/* Function to get user input of characters */
void fillS2(char * userStringPointer, char * randomStringPointer, char     replacementCharacter)
{

char userstring[21];
char randomString[41];
char copyString[42];
//char * pointerToCopyString = copyString;

userStringPointer = userstring;



int i = 0;
int n = 0;
int lowercaseCheck = 0;

char loopContinue = 0;




fillS1(randomStringPointer);  //here is the function call for the randomly generated       string.



printf("This is the random string: %s", randomStringPointer);

do {

    /* For loop to copy the first randomly generated string */
    for(i = 0; i < 42; i++)
        *(randomStringPointer + i) = copyString[i];

    randomStringPointer = copyString;

    i = 0;
    lowercaseCheck = 0;

    /* While loop to to get user input */
    printf("Please enter at least 2 capital letters and a maximum of 20.\n");
    while  ((((*(userStringPointer + i)) = getchar()) != '\n'))  {

        /* Counter to determine how many characters were entered */
        i++;

    }

    /* Adding 1 to add to add null character */
    i++;

    *(userStringPointer + i) = '\0';

    //printf("This is the user's string %s", userStringPointer);

    /* Capital letter check */
    for (n = 0; n < 20; n++) {
        if (((*(userStringPointer + n)) >= 'a') && (*(userStringPointer + n) <= 'z')) {

            lowercaseCheck++;
        }
    }

    if (--i < 3) {
        printf("You need at least two letters\n");
    }

    else if (i > 21){
        printf("You cannot have more than twenty letters\n");
    }

    else if (lowercaseCheck == 0) {

        puts(userStringPointer);

        printf("Enter a character to replace occuring letters.\n");
        scanf("%c", &replacementCharacter);
        getchar();

        //printf("this is the copy string before strFilter: %s", randomStringPointer);
        //printf("This is the replacement character %c", replacementCharacter);

        strFilter(randomStringPointer, userStringPointer, replacementCharacter);

    }

    else

        printf("You  must have 2 capital letters.\n");


    printf("Would you like to enter another string (y/n)?\n");
    loopContinue = getchar();
    getchar();


} while (loopContinue != 'n' && loopContinue != 'N');

}

/* Function to replace letters with the character chosen by the user */
void strFilter(char * replacementCopyStringPointer, char * replacementUserStringPointer,         char c)
{

int i = 0;
int n = 0;

while (n < 20) {

    for (i = 0; i < 40; i++) {
        if ((*(replacementCopyStringPointer + i)) == *(replacementUserStringPointer + n)){
            *(replacementCopyStringPointer + i) = c;
        }
    }
    i = 0;
    n++;
}

puts(replacementCopyStringPointer);
}
user1681673
  • 368
  • 1
  • 6
  • 28
  • `fillS2` takes a `char *` parameter named `userStringPointer`, but then it immediately changes where `userStringPointer` points to. This means that it's totally useless to take it as a parameter. (Maybe you're expecting pass-by-reference behavior?) – ruakh Nov 07 '12 at 01:03
  • Thank you for the comment. That actually cleared a lot up for me. – user1681673 Nov 07 '12 at 01:23

1 Answers1

1

randomlyGeneratedArray array in fillS1 would get destroyed once fillS1 function returns. You should allocate the memory from heap for randomlyGeneratedArray.

randomlyGeneratedArray = (char *)malloc(sizeof(char)*41)

Also do the same for userStringPointer in fillS2. That should solve the problem.

For difference between stack and heap read this question What and where are the stack and heap?

Community
  • 1
  • 1
Harman
  • 1,571
  • 1
  • 19
  • 31