2

So I am trying to figure out a way to print a string without spaces and it is not working for some reason, whenever I enter in a string to the program it ends out printing nothing.

#include <stdio.h>

char *removeSpaces(char *inString);

int main() {
    char str1[50];
    printf("Enter a string : ");
    scanf("%s", &str1);
    removeSpaces(str1);
}

char *removeSpaces(char *inString) {
    char str2[50];
    int j = 0;

    for (int i = 0; i < 50; i++) {
        if (inString[i] != ' ') {
            str2[j] = inString[i];
            j++;
        }
    }

    for (int i = 0; i < 50; i++) {
        printf("%s", str2[i]);
    }
}
10
  • 1
    Do not use & with arrays in scanf ==> scanf("%49s", str1); Commented Apr 9, 2020 at 14:32
  • 3
    In removeSpaces() print characters with printf("%c", str2[i]); Commented Apr 9, 2020 at 14:34
  • %s in scanf() will look for the next whitespace character and ends the input. Use %[^\n]. Commented Apr 9, 2020 at 14:48
  • @pmg hey so i tried printing it, it shows the first segment fine but when it reaches the space it prints garbled text Commented Apr 9, 2020 at 14:49
  • 1
    Change the printing to for (int i = 0; str2[i] != '\0'; i++) printf("%c", str2[i]); OR simply get rid of the for loop and just do printf("%s", str2); Commented Apr 9, 2020 at 14:56

2 Answers 2

3

Wrong input approach

Below will not scan into str1 anything with a space.

// bad
char str1[50];
scanf("%s", &str1);

Instead, use fgets() to read a line and form a string.

char str1[50];
if (fgets(str1, sizeof str, stdin)) {
  // success!

Lop off the potential trailing '\n' if desired.

  str1[strcspn(str1, "\n")] = '\0';

Reads past end of string

The loops reads to the size of the array. It should loop to the null chracter.

// for (int i = 0; i < 50; i++)
for (int i = 0; inString[i]; i++)

Missing \0

The string formation in str2[] is incomplete as it lacks a null chracter '\0'.

str2[j] = '\0'; // add after the loop

Warnings not fully enabled

Below should warn about mis-match of "%s" with str2[i].

for (int i = 0; i < 50; i++) {
    printf("%s", str2[i]);
}

Instead, without a loop.

 printf("%s", str2);

This is the biggest lesson to learn here. By fully enabling warnings, the compiler provides rapid feedback that something is wrong or questionable; faster than Stackoverflow.

Missing return

char *removeSpaces(char *inString) is expected to return a char *, yet code lacks a return something;.

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

Comments

3

You can also use pointers to walk memory locations testing each position for unwanted char value, Here is your function modified to use that method:

char *removeSpaces(char *inString);// your original prototype
char *remove_char(char *inString, char c);//generalized  

int main(void) {
    //use suggestions in other answer for correct user input
    const char str1[] = {"this is a string with spaces"};//for simple illustration

    printf("%s\n", removeSpaces(str1));
    printf("%s\n", remove_char(str1, ' '));
    return 0;
}

char *removeSpaces(char *inString)// your original prototype
{
    if(!inString) return (char *)"bad input";
    char *from; // "read" pointer
    char *to; // "write" pointer

    from = to = inString; // init both read and write pointers to original string

    while (*from) 
    { 
        if( (*from != ' ') && (*from != '\t') && (*from != '\n'))
        { 
            *to++ = *from; 
        } 
        from++; 
    } 
    *to = 0; 

    return inString;// add a return statement to return the char *
}

//optionally, create your function to remove any unwanted character
char *remove_char(char *inString, char c)//generalized  
{
    char *from; // "read" pointer
    char *to; // "write" pointer

    from = to = inString; // init both read and write pointers to original string

    while (*from) 
    { 
        if (*from != c) 
        { 
            *to++ = *from; 
        } 
        from++; 
    }  
   *to = 0; 

    return inString;// add a return statement to return the char *
}

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.