0

Hey just doing some exercises in c, one is saying to replace tabs in the input string with any other characters , i restrict myself to only using getchar(), no gets() fgets() etc..., as my learning book didn't catch it yet, so i tried to not break the flow, the code below just printf() the same line it receives, can you please examine why ?

#include <stdio.h>



int main(){

        char line[20];
        char c;
        int i = 0;
        printf("Enter name: ");
        while ( c != '\n'){
                c = getchar();
                line[i] = c;
                ++i;}
        while (line[i] != '\0')
                if (line[i] == '\t')
                        line[i] = '*';

        printf("Line is %s \n", line);

        return 0;} 

 
Question
  • 63
  • 7

1 Answers1

1
  • c, which is used in c != '\n', is not initialized at first. Its initial value is indeterminate and using is value without initializng invokes undefined behavior.
  • You are checking line[i] != '\0', but you never assigned '\0' to line unless '\0' is read from the stream.
  • You should initialize i before the second loop and update i during the second loop.
  • Return values of getchar() should be assigned to int to distinguish between EOF and an valid character.
  • You should perform index check not to cause buffer overrun.

Fixed code:

#include <stdio.h>

#define BUFFER_SIZE 20

int main(){

        char line[BUFFER_SIZE];
        int c = 0;
        int i = 0;
        printf("Enter name: ");
        while ( i + 1 < BUFFER_SIZE && c != '\n'){
                c = getchar();
                if (c == EOF) break;
                line[i] = c;
                ++i;
        }
        line[i] = '\0';
        i = 0;
        while (line[i] != '\0'){
                if (line[i] == '\t')
                        line[i] = '*';
                ++i;
        }

        printf("Line is %s \n", line);

        return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Hey @MikeCAT thanks for the valuable explanations , i literately wrote it in my notebook , i still have 2 questions though: 1) why ```i+1 – Question Jun 15 '21 at 16:55
  • the lessons were amazing but your code still give the same input line indeed. – Question Jun 15 '21 at 17:04
  • @RidaEN-Nasry [This code works for me](https://wandbox.org/permlink/BGDuUhs4PAV8JoQN). – MikeCAT Jun 15 '21 at 17:06
  • @RidaEN-Nasry 1) `i+1 – MikeCAT Jun 15 '21 at 17:10
  • thanks a lot @MikeCAT one last question, when we assigned ```line[i] = '\0' ```, supposedly it was added as the last element to ```line[i]```? how's that ? i mean do array[i] or the i-th element of any array always refer to the last one? – Question Jun 15 '21 at 21:02
  • @RidaEN-Nasry `line[i] = '\0'` is assigning `'\0'` as the last character of the string because `i` is updated before that to make `line[i]` represent one element after the last assigned element. Generally `array[i]` is just an i-th element of `array` and there are no guarantee that it is the last element. – MikeCAT Jun 15 '21 at 22:55