putchar() function: ambiguous output

740 Views Asked by At

Here is a simple code , trying to clear spaces from character array, but output is not like I did expect "YasserMohamed".

#include<stdio.h>

int main()
{
    char x[]="Yasser Mohamed";
    char ch;
    int i=0;
    while (x[i]!='\n')
    {
        if(x[i]!=' ')
            putchar(x[i]);
        i++;
    }
    system("pause");

    return 0 ;
}
7

There are 7 best solutions below

1
On BEST ANSWER

There's no newline ('\n') in x. So, the condition is wrong and it should be:

while (x[i]) /* until the null byte is found */
{
    if (x[i] != ' ')
        putchar(x[i]);
    i++;
}
1
On

Your string in x does not contain the newline character '\n' that you use as a condition in the loop.

Use while (x[i]!=0x00) to end at the terminating NUL character (0x00).

0
On

There is no \n in your original x, so you just continue iterating uninitialized memory until you happen to encounter \n. Instead, you should iterate up to the string terminating character - \0:

while (x[i] != '\0') {
// Here --------^
0
On

You can also use 0 instead of '\ 0'(is exactly the same value), like that:

for (int i = 0; x[i] != 0; i++) {
    if (x[i] != ' ')
        putchar(x[i]);
}
0
On

there is a null character at the end of a null terminated string not a new line.

you should change '\n' to '\0' or 0(which is the ASCII code of null character).

 #include<stdio.h>


 int main()
 {

     char x[]="Yasser Mohamed";
     char ch;
     int i=0;
     while (x[i]!='\0')
     {
         if(x[i]!=' ')
             putchar(x[i]);
         i++;
     }
     system("pause");

     return 0 ;

 }
0
On

It is because you never stopped the loop you wrote

while(x[i]!='\n')
    {
       //What You Want To Do.
           }

But x[i] was not '\n' for any x[i] defined.

It would work if you instead write it as i!= 14. Then Loop Would stopped at end of your name. Going Beyond is undefined as this not your variable memory region.

Or You Could also write while(x[i]) as end of the string in C are Null-Terminated \0 which evaluates to false, so loop would stop.

Correct Code Could Be

 #include<stdio.h>
 int main()
 {

     char x[]="Yasser Mohamed";
     char ch;
     int i=0;
     while (x[i])    //As Null Character '\0' evaluates to false it would stop the loop
     {
         if(x[i]!=' ')
             putchar(x[i]);
         i++;
     }
     system("pause");

     return 0 ;

 }
0
On

Updated code:

int main()
{
    char x[]="Yasser Mohamed";
    char ch;
    int i=0;
    while (x[i]!='\0')
    {
        if(x[i]!=' ') {
            printf("%c", x[i]); // replace putchar with printf
            fflush(stdout); // force character to appear
        }
        i++;
    }
    printf("\n"); // print newline so shell doesn't appear right here
    return 0 ;
}

Strings are terminated with null \0 characters not newlines.

Also, you should add an fflush statement (at least on linux) to make sure every character gets printed.

To make your output look nice, add a newline after the loop.

I replaced your putchar call with printf to see if that would help when I ran your program. putchar will likely also work fine.

I removed system(pause) because it didn't seem to help. I added a newline character print instead.