-1

I'm trying to solve UVAa Online Judge Problem 272 — TeX Quotes.

Input will consist of several lines of text containing an even number of double-quote (") characters. Input is ended with an end-of-file character. The text must be output exactly as it was input except that:

  • the first " in each pair is replaced by two ` characters: `` and
  • the second " in each pair is replaced by two ' characters: ''.

I don't know why my code gives the wrong answer; I think it's the right answer.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main(){
    char kalimat[100000];
    int i;
    int max;
    bool flag=true;
    while (scanf("%c",&kalimat)!=EOF){
        max=strlen(kalimat);
        for (i=0;i<=max;i++){
            if (kalimat[i]=='"')
            {
                if (flag==true){
                    printf("``");
                    flag=false;
                } else {
                    printf("''");
                    flag=true;
                }
            } else {
                printf("%c",kalimat[i]);
            }
        }
    }
    return(0);
}
Community
  • 1
  • 1
Felics
  • 1
  • 3
  • Please don't just link to the problem: make your question self-contained, that is: summarise the problem in your question, add the expected outcome and the actual outcome. You can always add the link for extra information. –  Jun 26 '15 at 02:34
  • You mention: "I don't know why my code gives the wrong answer; I think it's the right answer." Well, what is the answer you get? What answer should it have been (according to the problem posed? –  Jun 26 '15 at 02:35
  • Have you actually tried debugging your program by putting eg printf statements in your code, to see what values `kalimat`, `max` and `i` have? –  Jun 26 '15 at 02:46

1 Answers1

0

Note that scanf("%c",&kalimat) will read only 1 (one) character at a time. So strlen(kalimat) will always be 1. Not an actual problem, but just odd (e.g., you could declare char kalimat, instead of an char array, and not use indexing or the for loop).

Your for-loop, however, goes from 0 to max inclusive, thus kalimat will be indexed out-of-bounds, and result in undefined behaviour. Perhaps your problem is there.

In fact, since kalimat is a single character, it won't have a terminating '\0' character, and thus is not a valid C-string. Hence strlen can never compute the correct length (which is 1).

Try this:

char kalimat;
...
while (scanf("%c", kalimat) != EOF) {
        if (kalimat == '"') {
            if (flag){
                printf("``");
                flag = false;
            } else {
                printf("''");
                flag = true;
            }
        } else {
            printf("%c", kalimat);
        }
    }
}
  • There's no guarantee that `strlen(kalimat) == 1`; there could be gibberish in the buffer because it isn't initialized (though some platforms will have zeroed bytes there) – Jonathan Leffler Jun 26 '15 at 03:18
  • @JonathanLeffler That is true. That first pararagraph was written before I realised the missing null terminator, after which I wrote the third paragraph. Of course, it's also why `strlen` is removed from the suggested code. –  Jun 26 '15 at 03:37