0

I'm not sure if I'm just being really dumb but this function I have made should take chars from one array and selectively put them in another to remove things like spaces and punctuation. However, what returns seems to just be the first char (from the printf).

char * getWord(char *array) {
    char *temp = malloc(sizeof(char) * 20);
    int i= 0;
    while(i < 20) {
        if(validChar(array[i])) {
            printf("pass 1 - %c\n", array[i]);
            temp[i] = array[i];
            printf("pass 2 - %c\n", temp[i]);
            i=i+1;
        }
        else {
            i=i+1;
       }
    }
    printf("%s\n", temp);
    return temp;
}

The validChar function:

bool validChar(char given) {
    char a[]=". ,;:*!?'-\n\r";
    for(int q = 0; q <=12; q++) {
        if(given == a[q]){
            return false;
        }
    }
    return true;
}

In each "pass 2", the correct print is shown so the loop is working as intended. The thing I really don't understand is when I remove the conditional to select valid chars, the final print statment works as intended and prints the entire string.

2
  • Shoudn't the for loop in validChar run only to 11? Commented Nov 28, 2015 at 18:46
  • "However, what returns seems to just be the first char (from the printf)." - Oh, really? show code which reproduces this behaviour. Commented Nov 28, 2015 at 18:46

2 Answers 2

1

First, you're using just one counter for both strings in getWord. You have to separate the reading of the string with i of the writting of the temp string with another counter, say j. j will only be incremented when you actually add some character to temp.

Finally, you're missing the ending '\0' of temp before the printf in getWord.

Sign up to request clarification or add additional context in comments.

Comments

1

You need two counters one for each array. Counter i, which you already use for array, and a new one j which will be used for temp.

    if(validChar(array[i])) {
        printf("pass 1 - %c\n", array[i]);
        temp[j] = array[i];
        printf("pass 2 - %c\n", temp[j]);
        i=i+1;
        j=j+1;
    }
    else {
        i=i+1;
   }

The reason you only printed the first character is that the memory you got from malloc happened to contain bytes set to zero. The first character that got skipped in temp, contained a zero byte, since a string is zero terminated, that is all you printed out.

Technically those zero bytes were not initialized so the behavior of your code is undefined.

The code also doesn't zero terminate the string temp, after the copying is completed. Add an assignment of '\0' to temp at position j after the while loop, and change the while loop condition from 20 to 19 (or allocate one more byte), so the zero terminator won't be written out of bounds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.