简体   繁体   中英

C : trying to remove spaces from a string without using any string library functions

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]);
    }
}

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 *
}

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; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM