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);
}
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. Hencestrlen
can never compute the correct length (which is 1).Try this: