2

I need to take only the odd values from a char array and copy them into correctly sized dynamic memory using a pointer.

However when running my program it works correctly with certain input strings and not with others. Is there something that I'm doing wrong? I can't seem to figure out what's going on.

/* A.) Include the necessary headers in our program */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_LENGTH 32

int main() {
    /* B.) Declare char array with inital size of 32 */
    char input_string[MAX_STRING_LENGTH];

    /* C.) Recieve user input.
           Can save the first 31 characters in the array with 32nd reserved for '\0' */
    printf("Enter a string of characters: ");

    /* D.) Using the technique we discussed to limit the string to 31 charaters */
    scanf("%31s", input_string);
    printf("\n");

    /* Will be used to determine the exact amount of dynamic memory that will be allocated later */
    int odd_value_count = 0;
    printf("Odd Characters: ");
    for(int i = 0; i < strlen(input_string); i++) {
        if(i % 2 != 0) {
            printf("%c ", input_string[i]);
            odd_value_count++;
        }
    }

    printf("\n");
    printf("Odd value count: %d\n", odd_value_count);

    /* E.) Delecaring the pointer that will hold some part of the input_string
           Pointer will be a char type */
    char *string_pointer;

    /* G.) Allocating the space before the copy using our odd value count */
    /* H.) The exact amount of space needed is the sizeof(char) * the odd value count + 1 */
    string_pointer = (char *)malloc(sizeof(char) * (odd_value_count + 1));

    if (string_pointer == NULL) {
        printf("Error! Did not allocte memory on heap.");
        exit(0);
    }


    /* F.) Copying all charcters that are on the odd index of the input_string[] array
           to the memory space pointed by the pointer we delcared */
    printf("COPIED: ");
    for (int i = 0; i < strlen(input_string); ++i) {

        if(i % 2 != 0) {
            strcpy(string_pointer++, &input_string[i]);
            printf("%c ", input_string[i]);
        }
    }

    /* Printing out the string uses the pointer, however we must subtract odd_value_count to
       position the pointer back at the original start address */
    printf("\n%s\n", string_pointer - odd_value_count);

    return 0;

}

This input string: 01030507 works fine and copies & prints: 1357

The input string: testing Copies etn but prints etng.

I cant figure out why for some strings it prints out the extra character at the end when I never even copy the value over.

2
  • "I can't seem to figure out what's going on" -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: What is a debugger and how can it help me diagnose problems? You may also want to read this: How to debug small programs?. Commented Sep 21, 2020 at 19:44
  • If I wasn't debugging it myself then I wouldn't post here. The point is after debugging I don't understand wants going on. Thanks though. Commented Sep 21, 2020 at 19:46

2 Answers 2

2

You need to Null Terminate your string, like this *string_pointer = '\0';, just after you are done copying the odd characters in your string pointer - after that loop, null terminate your string.

Read more in How to add null terminator to char pointer, when using strcpy?

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

5 Comments

Read through that an solved the problem. Thank you.
@JustinCabral glad I was able to help.
@JustinCabral, you should accept this answer, as it was the first to point out the problem. I'm also glad I could help, though not as much :)
Yes indeed I was the first to answer, but your answer shows a more complete code snippet's than mine. @JustinCabral I encourage you to accept the answer that you feel will help more the future reader, the person that in the future has the same problem as you! :)
@JustinCabral: You can upvote the answer that you didn't accept, so both answerers will get a similar amount of reputation (10 points and 15 points). However, the future reader will normally see the accepted answer first.
2

In the end of your routine you will need to null terminate the string, otherwise you don't have a string you just have a char array, you can use string_pointer which is already pointing to one past the end of the string you want to save:

//...
for (int i = 0; i < strlen(input_string); ++i) {

    if(i % 2 != 0) {
        strcpy(string_pointer++, &input_string[i]);
        //as you are copying characters, you can do this:
        //*string_pointer++ = input_string[i]; 
        //instead of strcpy
        printf("%c ", input_string[i]);
    }
}
*string_pointer = '\0'; // <-- here
//...

1 Comment

Yes this is exactly what I needed to do. Thank you.

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.