0

I want to remove any spaces from the user input and give the result back on the screen. So far, the following is my working solution. I haven't noticed any errors yet. Since I'm pretty new to C and programming in general, my question is: Is there something I can do better? Anything to optimize or something? I appreciate any tips from you guys since you are probably a lot more experienced than I am. So, here's my code:

#include <stdio.h>
#include <string.h>
#define PUFFERGROESSE 100
#define ERROR 1
#define OK 0

int main(){

    char stringPuffer[PUFFERGROESSE];
    printf("Please enter some words:"); fflush(stdout);
    if(fgets(stringPuffer, PUFFERGROESSE, stdin) == NULL){
        printf("Unable to read.\n");
        return ERROR;
    } else {
        char endString[PUFFERGROESSE];

        for (int i = 0, j = 0; i < PUFFERGROESSE; i++, j++) {
            if (stringPuffer[i] != ' ' ) {
                endString[j] = stringPuffer[i];
            } else {
                j--;
            }
        }


        printf("Without spaces your input looks like that: %s", endString);
    }
}
4
  • 1
    Questions regarding code efficiency, as opposed to not-working code, are best asked at Code Review. Commented Nov 5, 2015 at 16:22
  • Thank you, I didn't know that website! Commented Nov 5, 2015 at 16:23
  • 1
    instead of messing around with j you could more simply do endString[j++] = stringPuffer[i]; but don't forget to terminate the string if you use strlen Commented Nov 5, 2015 at 16:23
  • You could get rid of endString all together: for (int i = 0, j = 0; i < strlen(stringPuffer); i++) if (' ' != stringPuffer[i]) stringPuffer[j++] = stringPuffer[i]; Then add an '\0' at the end. Commented Nov 5, 2015 at 16:32

2 Answers 2

3

In your code, the for loop condition is i < PUFFERGROESSE. Inside this loop, you access stringPuffer using the loop index.

Now, stringPuffer being an uninitialized automatic local variable and with a sufficiently small input, a strict check like i < PUFFERGROESSE will cause access to uninitialized memory of stringPuffer, creating undefined behavior.

You can make use of strlen() after taking the user input.

Another note, int main() is better as int main(void), at least.

NITPICK: why's the OK defined, if not used?

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

1 Comment

Thank you! I'll try to implement that in my code later that day.
1

Several suggestions:

  1. Initialize endString to all zeros; that way you won't have to worry about string termination issues later on:
    char endString[PUFFERGROESSE] = {0};
    
  2. Instead of looping while i is less than PUFFERGROESSE, loop until you see the end of the string:
    for( int i = 0, j = 0; stringPuffer[i] != 0; i++ )
    
  3. Also, only increment j when you write the non-space character, rather than incrementing it unconditionally and then having to decrement it when you see a space:
    if ( !isspace( stringPuffer[i] ) )
      endString[j++] = stringPuffer[i];
    

So basically, that code reduces to:

char endString[PUFFERGROESSE] = {0};

for (int i = 0, j = 0; stringPuffer[i] != 0; i++) {
  if ( !isspace( stringPuffer[i] ) ) {
      endString[j++] = stringPuffer[i];
  }
}

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.