Copying input to output without unnecessary Spaces in C

c program to eliminate white spaces
c++ program to remove extra spaces
c program to remove blank lines from a file
given a line of text as input, output the number of characters excluding spaces, periods, or commas
python remove extra spaces between words
how to check space in string in c
write a program that replaces two or more consecutive blanks in a string by a single blank
count input length without spaces, periods, or commas python

I'm trying to write a program in C that copies its input to its output while replacing each string of one or more Spaces with a single Space. My code isn't doing that but is instead taking away every second character. This is my code:

#include <stdio.h>

main()
{
    int c;
    int lastc;

    lastc = 0;

    while(getchar() != EOF){
        c = getchar();
        if(c == 32 && lastc == 32)
            ;
        else
            putchar(c);
        lastc = c;
    }
}

Your loop should look like:

while((c = getchar()) != EOF){
    if(c == 32 && lastc == 32)
        ;
    else
        putchar(c);
    lastc = c;
}

In your version you get a char with getchar while checking the condition for the while loop and then as a next step you again get a char with getchar. So the first one is not used in your code. Therefore it is taking away every second character.

C program remove spaces, blanks from a string, If you want you can copy blank into text string so that original string is modified. Download Remove spaces program. Output of program: Remove spaces c  Logic to remove extra spaces, blanks. Input string from user, store it in some variable say str. Declare another string newString with same size as of str, to store string without blanks. For each character in str copy to newString. If current character in str is white space. Then copy single white space to newString and rest white spaces in str.

Keep running in while loop until you get non-space character and print just one space after you get out.

int main()
{
    int c;
    bool space=false;

    while ((c=getchar()) != EOF) {
        while (isspace(c)) {
            space = true;
            c = getchar();
        }
        if (space) {
            putchar(' ');
            space = false;
        }
        putchar(c);
    }
    return 0;
}

Remove extra spaces from a string, Input: str = "GeeksforGeeks"; Output: "GeeksforGeeks" (No change is needed) On finding consecutive space characters, one only space is copied to the location of the first pointer and rest C++ program to implement custom trim() function. C program to remove spaces or excess blanks from a string, For example, consider the string "C programming" There are two spaces in this string, so our program will print the string "C programming." It will remove spaces when they occur more than one time consecutively in string anywhere.

I use fgets() function to getting string from input i.e stdin and store in the scroll string.

Then you must implement a way to analyze string to find spaces in it.

When you find first space, increase index if you face another space.

This is the code.

Code

#include <stdio.h>

int main(void){

    char scroll[100];// = "kang  c    heng    junga";
    fgets(scroll, 100, stdin);
    printf ("Full name: %s\n", scroll);


    int flag = 0;
    int i=0;
    while (scroll[i] != '\0')
    {
        if (scroll[i] == ' ' )
            flag=1;//first space find

        printf("%c",scroll[i]);

        if (flag==0){       
                i++;
        }else {
            while(scroll[i]==' ')
                i++;
            flag=0;     
        }

    }

    return 0;
} 

Sample input: Salam be shoma doostane aziz

Program output: Salam be shoma doostane aziz

[Edit]

Use new string st to hold space eliminated string an print as output.

Also this code work for Persian string.

char scroll[100]={0};// = "kang  c    heng    junga";
printf("Enter a string: ");
fgets(scroll, 100, stdin);

printf ("Original string: %s\n", scroll);

char st[100]={0};

int flag = 0;
int i=0;
int j=0;

while (scroll[i] != '\0')
{
    if (scroll[i] == ' ' )
        flag=1;//first space find

    st[j]=scroll[i];
    j++;    

    if (flag==0){       
            i++;
    }else {
        while(scroll[i]==' ')
            i++;
        flag=0; 
    }

}

printf("Eliminate Spaces: %s", st);

C program to remove spaces, blanks from a string, Output String after removing extra blanks: "Learn C programming at Must know - Program to copy one string to another. Logic to remove extra spaces, blanks. Input string from user, store it in some variable say str. Declare another string newString with same size as of str, to store string without blanks. We can take string input in C using scanf(“%s”, str).But, it accepts string only until it finds first space. There are 3 method by which C program accepts string with space in the form of user input.

Edpac: Utility Programs for Computer-assisted Editing, , TO MAKE RIGHT HAND JUSTIFIED LINJUAT 10 EXTRA SPACES ARE JUAT 11 JUAT 12 THE FIRST INPUT DATA CARD , STORED IN THE CONTAINS THE ALPHABET JUAT 17 THEREFORE A ( 3 ) IS ASSUMED TO BE C , A ( 4 ) TO JUAT 23 A ( 45 ) IS USED TO INDICATE THAT THE OUTPUT LINE IS JUAT 24​  C Program to Copy Text From One File to Other File; C Program to Compare two Text or Data files; C program to Convert the file contents in Upper-case & Write Contents in a Output file; C Program to Read last n characters from the file ! C program to Copy the contents of one file into another using fputc; C Program to read the content of file

C Program for removing Whitespaces in a String, C Program to remove whitespaces in a string with explanation. the string: "); gets(aj); len = strlen(aj); // len stores the length of the input string while(aj[i] != If non-space character is full stop, comma or a question mark, we also remove any preceding space before it. On finding consecutive space characters, one only space is copied to the location of the first pointer and rest are ignored. The leading and trailing spaces are handled separately in the solution. Below is C++ implementation of above idea.

Document Formatting and Typesetting on the UNIX System, line-indent instruction name effect specifies line adjustment: c=l (left-adjusted), c=​r n.f no-fill mode copies input to output without line adjustment n turns off the . sp Its restore spacing turns on the .sp and .bp instructions ti [[+]n ) temporary-​  Output. C Programming. How does this program work? All valid C programs must contain the main() function. The code execution begins from the start of the main() function. The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations.