mardi 30 juin 2015

Reversing a two dimensional character array in C


i am trying to write a program which reverses a entire string and also may print reversing each word of the string more than one time. For example my string is: "2 Fox jumps over the lazy dog." for this, the output should be: .god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF. I am trying to store each word in a 2d array and then reverse each word but i am not able to do that. here is my code. kindly help Note: how do we provide EOF in console

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

int main() {
    char string[100][100];
    char ch;
    int i = 0, j = 0, l = 0, count = 0, x = 0, y = 0;
    while ((ch = getchar()) != EOF) {
        string[i][l++] = ch;
        if (ch == ' ') {
            string[i][l] = '\n';
            i++;
            l = 0;
            count++;
        }
    }
    for (x = 0; x <= count; x++) {
        int length = strlen(string[x]) - 1;
        for (y = length; y >= 0; --y)
            printf("%s", string[y]);
    }
    return 0;
}


Aucun commentaire:

Enregistrer un commentaire