ADBlock is blocking some content on the site

ADBlock errore

Can't get a function call in a function to work properly

StackOverflow https://stackoverflow.com/questions/13215864


I'm writing a program to generate a string of random uppercase letters, then take user input of uppercase letters, along with a character form the user. For any instance of the user input letter in the random string, it replaces that letter with the character entered by the user.

For example, s1 = {BDHFKYL} s2 = {YEIGH} c = '*'

Output = BD*FK*L

The program was working correctly until I added the feature to ask the user to enter what character they would like to replace the letters.

The output is:

Please enter at least 2 capital letters and a maximum of 20.
Enter a character to replace occuring letters.
Would you like to enter another string?

Here's the code:

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[42];
 char s2[22];
 char x = 0;

 fillS2(s2, s1, x);

 return 0;

void fillS1(char x[])
 for (int i = 0; i < 40; i++)
     x[i] = 'A' + random() % 26;
 x[40] = (char)0;

void fillS2(char x[], char y[], char z){

 char loopContinue = 0;

 do {

 int i = 0;
 int capitalLetterCheck = 0;

 printf("Please enter at least 2 capital letters and a maximum of 20.\n");
 while (( x[i] = getchar()) != '\n' ) {



 x[i] = '\0';

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

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

for (i = 0; i < 20; i++) {
        if ((x[i] >= 'a') && (x[i] <= 'z')) {
            printf("You many only have capital letters.\n");
            capitalLetterCheck = 2;

if (capitalLetterCheck != 2) {
    for (i = 0; i < 20; i++) {
        if ((x[i] >= 'A') && (x[i] <= 'Z')) {


            printf("Enter a character to replace occuring letters.\n");
            while ((z = getchar() != '\n')) {


            strFilter(y, x, z);  

    printf("Would you like to enter another string?\n");

} while (loopContinue != 'n');


void strFilter(char a[], char b[], char c){
 int i = 0;
 int n = 0;

 while (n < 20) {

        for (i = 0; i < 40; i++) {
            if (a[i] == b[n]){
                a[i] = c;

    i = 0;


Thank you.

No correct solution


first of all please try to make your code a little easier to read, and I'm not talking about indenting but about its flow.

Also, your example output seems to work fine since there was nothing to change in any string here...?

There are a few things you should keep in mind when coding :

  • give your variables and functions explicit names, espcecially if you are going to have someone read your code at some point
  • try to keep the flow of your code simple by making small functions when you have a specifig task to execute (get the user's input, generate a random string, etc.) as opposed to just writing most of it in imbricated loops
  • You could also have a look at scanf (man scanf) to get the user's input
  • Try allocating a buffer when you get the user's input instead of having a static one that may not be of the right size

It's very easy to write some pseudo-code and then translate it into C :

WHILE someCondition
    Generate a random string
    Get a string from the user
    Get a character from the user
    Find and replace

Here is an example of how you could have organised your code (don't use it though - no frees, no getting the user's input, etc.) :

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

char* generateString(void)
    return "AEIOUYAEIOUY"; // In your implementation, this is where you'd generate the random string

char* getStringInput(void)
    return "HELLO"; // In your implementation, this is where you'd get the user's string

char getCharInput(void)
    return '*'; // In your implementation, this is where you'd get the user's character

char* findAndReplace(char* randomString, char* userString, char userChar)
    int l1;
    int l2;
    int i;
    int j;
    char* output;

    l1 = strlen(randomString);
    l2 = strlen(userString);
    output = (char*)malloc(sizeof(*output) * l1);
    strcpy(output, randomString);
    for (i = 0; i < l1; ++i)
        for (j = 0; j < l2; ++j)
            if (randomString[i] == userString[j])
                output[i] = userChar;

    return (output);

int main(int ac, char** av)
    char* randomString;
    char* userString;
    char userChar;
    char* outputString;

    randomString = generateString();
    userString = getStringInput();
    userChar = getCharInput();
    outputString = findAndReplace(randomString, userString, userChar);
    printf("Result: %s\n", outputString);

    // don't forget to free any allocated buffer

    return (1);

How much debugging have you done? Try putting some printfs in your code to see what happens - when functions are called, what are your variable's values, etc. Example :

void fillS1(char x[])
 printf("-- entering fillS1, buffer value: %s\n", x);
 for (int i = 0; i < 40; i++)
     x[i] = 'A' + random() % 26;
 x[40] = (char)0;
 printf("-- leaving fillS1, buffer value: %s\n", x);

(be careful about what's in your buffer before you use printf)

This should tell you pretty quickly what's going wrong.

For example, try checking the value of "c" in strFilter when it's called, and have a second look at how you get the user's input.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow