This is a problem from the Data Structures and Algorithms textbook by Thareja. I am trying to solve the problems to be prepared for my Data Structures class. I am compiling and running this at https://www.onlinegdb.com/online_c_compiler. My program is coming to a segmentation fault and it is never entering the if statement(I cannot seem to find out why). The issue is possibly trivial and I am overlooking it but I would like another set of eyes to take a look at it.
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char str[100],ans[100];
int i=0,j=0;
clrscr();
printf("\nEnter string: ");
gets(str);
while(str[i]!='\0')
{
if(str[i]==' ')
{
i++;
continue;
}
ans[j]=str[i];
j++;
}
ans[j]='\0';
printf("\nThe string is: ");
puts(ans);
getch();
return 0;
}
Thanks for the help.