2

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 ;
}
alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    Consider Other Answe too for marking best answer. – Suraj Jain Dec 31 '16 at 12:21
  • the `system()` function requires the statement: `#include ` which the posted code is missing. – user3629249 Jan 01 '17 at 16:08
  • the `pause` shell command is not portable, It is only found on windows. a generic way to implement a similar functionality is: `int ch; while( (ch = getchar() != EOF && '\n' != ch ); getchar();` – user3629249 Jan 01 '17 at 16:10

7 Answers7

4

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++;
}
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
P.P
  • 117,907
  • 20
  • 175
  • 238
1

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

alk
  • 69,737
  • 10
  • 105
  • 255
Marc Balmer
  • 1,780
  • 1
  • 11
  • 18
  • 1
    "*Use `while (x[i]!=0x00)`*" or `while (x[i]!=0)` or `while (x[i]!='\0')` or just `while (x[i])`. – alk Dec 31 '16 at 12:03
1

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 ;

 }
Suraj Jain
  • 4,463
  • 28
  • 39
0

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 --------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

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]);
}
Amarildo
  • 268
  • 4
  • 19
0

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 ;

 }
Sajad Banooie
  • 350
  • 1
  • 7
0

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.