First error, as everyone has pointed out is the logical operator in the if()
statement. It should be
if(m!=' ' && m!='\n')
As you want to check if the entered character is neither a
(space) nor a \n
, so you have to use the &&
operator.
Next, the error you are getting is because of something called the trailing character
. When you enter a letter and press enter
(at the point where your scanf("%c",&m)
is asking for input). That letter gets stored in the variable m
, but the newline character \n
caused by the enter
pressed is still in the stdin
. That character is read by the scanf("%c",&m)
of the next iteration of the for
loop, thus the loop exits.
What you need to do is consume that trailing character
before the next scanf()
is executed. for that you need a getchar()
which does this job. So now your code becomes something like this..
#include<stdio.h>
int main()
{
char X[99];
char m;
int i;
printf("Type the string without spaces\n");
for (i=0;i<99;i++)
{
scanf("%c",&m);
if(m!=' ' && m!='\n')
{
X[i]=m;
getchar(); // this statement is consuming the left out trailing character
}
else
i=99;
}
printf("%s",X);
}
Now, the program works exactly as you want.