Mr BaW
Mr BaW

Reputation: 39

String appearing "broken" into parts after being printed in C code

I wrote this code in C where you type a word or sentence (char s[]) and two numbers (l for left and r for right). It's supposed to seperate the string into 3 parts: 1. from start to position l-1 ,2. from l to position r-1 and 3.from r to end. These are then appended to the temp string in this order: 3,2,1 and printed.Loop stops when input "quit" is given.Everything seems to work just fine ( i had the code checked by chatgpt) but for some reason when i upload the code to eagle(its the site we use at uni to grade our codes) the strings are printed divided ( part 3 and then part 2,1 ) and i just cant figure out what the problem is.

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
     int l, r, n, i;
     char s[1000];
     fgets(s, 1000, stdin);
     s[strcspn(s, "\n")] = '\0';
     while (strcmp(s, "quit") != 0) {
         scanf("%d %d", &l, &r);
         n = strlen(s);
         if (l < 0 || r >= n || l > r) {
            while (getchar() != '\n');
            continue;
         } 
         else {
             char *temp = (char *)malloc((n + 1) * sizeof(char));
             if (temp == NULL) {
                 return 1;
             }
             int j = 0;
             //printf("Copying Segment 1 (r..n): ");
             for (i = r; i < n; i++) {
                 temp[j++] = s[i];
                 //printf("%c", s[i]);
             }
             //printf("\n");
             //printf("Copying Segment 2 (l..r): ");
             for (i = l; i < r; i++) {
                 temp[j++] = s[i];
                //printf("%c", s[i]);
             }
             //printf("\n");

             //printf("Copying Segment 3 (0..l): ");
             for (i = 0; i < l; i++) {
                 temp[j++] = s[i];
                 //printf("%c", s[i]);
             }
             //printf("\n");
             temp[j] = '\0'; 
             printf("%s\n", temp);
             fflush(stdout);
             free(temp); 
         }
         while (getchar() != '\n'); 
         fgets(s, 1000, stdin);
         s[strcspn(s, "\n")] = '\0';
      }
      return 0;
 }

and this is the outcome from eagle: enter image description here

Upvotes: 1

Views: 64

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149135

Your code is not robust to bad conditions. This means that if you test it locally and do not try to give it strange inputs, everything will be fine.

But if for any reason you have non printable characters before the end of line (\n), you will leave them in your buffer and get spaces between the part 3 and the parts 1-2. And depending on IO configuration, lines could be ended with a "\r\n" string. That would leave a Carriage Return character between the parts 3 and 1. Or depending on the way you upload the input data, you could have space characters at end of lines. And they will give again a break between the parts 3 and 1.

If the same idea, you fail to test the return values of both fgets and scanf. If the input data is not what you expect that could be enough to get an infinite loop.

It may not be what beginners think about, because they are glad enough when the program produces the expected result when given the expected input. But the fact is that the difference between a nice and a poor program is generally the way they handle inexpected input data. A good program detects it and gives a relevant message. It is not important when you are experimenting on specific subjects... until things go wrong. As soon as you get an unexpected result, you shall add tests after every input function and add trace prints if it is not enough.

Upvotes: 0

Related Questions