-2

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.

  • 5
    First of all, [***never ever*** use the `gets` function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Use e.g. [`fgets`](http://en.cppreference.com/w/c/io/fgets) instead. Secondly, this is probably a very good time for you to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). For example, if you step through the code line by line in a debugger, you should be able to find out the error really quickly. – Some programmer dude Jul 28 '18 at 16:03
  • @Someprogrammerdude Only in real code. In practice problem where it's guaranteed that the input is less than a number of bytes then it's safe, but then, why developing bad practice? – user202729 Jul 28 '18 at 16:12
  • @Someprogrammerdude I am new to C. I am just following what the textbook has taught me. I have walked through my code multiple times and have come here as a last resort. Please help me out. – Anirudh Gottiparthy Jul 28 '18 at 16:14
  • Think how your counters are incremented ... What will happen when you hit first non-space character? – qrdl Jul 28 '18 at 16:20
  • @qrdl Thanks for explaining this. I get how it works now. It was an overlook on my part. – Anirudh Gottiparthy Jul 28 '18 at 16:30
  • To be frank, if the book you read told you to use `gets` then you should start looking for a more modern book that doesn't use it at all. – Some programmer dude Jul 28 '18 at 16:36

1 Answers1

0

To me it looks like the problem is with increment operator on variable i. Lets assume, the string user entered is not null and there is no leading space(s). In this case, your code enters while loop successfully (coz it couldn't find a null character) and next thing it checks if is a blank space (str[i]==' ') , which according to assumption is not so it moves on to store the character in in ans[j]. At this point everything looks okay but the program moves to next line your code increments j but what about i? By not incrementing i you're making while loop to enter an infinite loop. Hope this helps.

Karthick
  • 1,010
  • 1
  • 8
  • 24
RoT
  • 132
  • 6