Objective of Program : Get the number of strings to be entered, read the string, reverse the string and print the string. Proceed with the next string
#include <stdio.h> int main() { int num_tc, index_tc, char_idx, str_len = 0; char S[31]; scanf("%d\n", &num_tc); for (index_tc = 1; index_tc <= num_tc; index_tc++) { fgets(S, sizeof(S), stdin); /* To compute the string length */ for (char_idx = 0; S[char_idx] != NULL; char_idx++) str_len++; /* Reverse string S */ for (char_idx = 0; char_idx < str_len / 2; char_idx++) { S[char_idx] ^= S[str_len - char_idx - 1]; S[str_len - char_idx - 1] ^= S[char_idx]; S[char_idx] ^= S[str_len - char_idx - 1]; } puts(S); } return 0; }
Input to the program
2<\n> ab<\n> aba<\n>
Output
ba
Kindly let me know why the second string is not taken for string reversal.
- If I remove string reversal logic, I could see both strings output
Asked
Active
Viewed 322 times
1
1 Answers
2
You do not reset str_len
to 0
in the body of the loop. The length of the second string is incorrect, hence the second string is not properly reversed. Change the loop to:
for (str_len = 0; S[str_len] != '\0'; str_len++)
continue;
Note that you should strip the trailing '\n'
before reversing the string. You can do this with S[strcspn(S, "\n")] = '\0';
before computing str_len
.
Here is a simplified version using scanf()
, that reverses individual words:
#include <stdio.h>
int main(void) {
int num_tc, tc, len, left, right;
char buf[31];
if (scanf("%d\n", &num_tc) != 1)
return 1;
for (tc = 0; tc < num_tc; tc++) {
if (scanf("%30s", buf) != 1)
break;
/* Compute the string length */
for (len = 0; buf[len] != '\0'; len++)
continue;
/* Reverse string in buf */
for (left = 0, right = len - 1; left < right; left++, right--) {
buf[left] ^= buf[right];
buf[right] ^= buf[left];
buf[left] ^= buf[right];
}
puts(buf);
}
return 0;
}

chqrlie
- 131,814
- 10
- 121
- 189
-
@gopi_2363: can you mark this answer as accepted by clicking on the grey checkmark below the answer score? – chqrlie Jul 16 '16 at 21:13
-
Sure. 1) S[strcspn(S, "\n")] = '\0'; I am not sure will it be helping in this program. 2) A favour, can we write the same program with scanf (I tried with [link] (http://stackoverflow.com/questions/6282198/reading-string-from-input-with-space-character) ) 3) Is there a better way to put this scanf("%d\n",&num_tc); – gopi_2363 Jul 16 '16 at 21:16
-
You are supposed to reverse the line contents, prepending a linefeed and appending another linefeed with `puts()` seems inappropriate. Stripping the linefeed seems the proper semantics. The proposed method for that works in all cases. You could imagine other methods, such as testing for both `'\0'` and `'\n'` in the loop and forcing a `'\0'` at `S[str_len]`. – chqrlie Jul 16 '16 at 21:20
-
Reading the string with `scanf` will only read a single word. Your `scanf("%d\n",&num_tc);` seems correct, but it skips any white space after the number, effectively preventing you from inputing an empty line for the reverse phase. Mixing `scanf` and `fgets()` is tricky. It would be better to use `fgets()` for both lines and `sscanf()` to parse the number. – chqrlie Jul 16 '16 at 21:23
-
@chqlie : Would apprecate if you can share the same program (reading inputs would be sufficient) all with scanf. – gopi_2363 Jul 17 '16 at 13:04