1

I am trying to read the input to my program (a string of chars) and invert the order of the words that are in it.

For example, if I were to pass my program ABC DEF GHI JKL it would output JKL GHI DEF ABC. I am using the whitespace as separators.

My code:

char toReverse[1000];
char outputArray[1000];
int charCount = //Size of the toReverse array. Varies on the input
                //It is the total number of chars stored in the array
...
int i;
int tempCharCount = charCount;
int wordSize = 0;
int outputIndex = 0;
int sentenceIndex = 0;
int charStep = 0;
for(i = charCount-1; i>=0; i--){
    if(toReverse[i] == ' '){
        int j;
        sentenceIndex = tempCharCount - wordSize;
        for(j = 0; j<charStep; j++){
            outputArray[outputIndex++] = toReverse[sentenceIndex++];
        }
        outputArray[outputIndex] = ' ';
        outputIndex++;
        charStep = 0;
    }
    wordSize++;
    charStep++;

}

There is a flaw in my code. I do know why this happens though. For example, if I were to pass as input ABC DEF GHI, it will only output GHI DEF. This is because whenever the outer for loop reaches index 0 of my toReverse array, since it is not a space ' ', it does not do the if(toReverse[i]) inner for(j = 0; j<charStep; j++) since the condition is not met.

Do you have any advice regarding to the logic? I have tried reversing my logic, such as if(toReverse[i] != ' ') but it brings more problems than it solves.

Thanks for your advice and comments!

Cheers

Edit 1


I am reading my input from a file By the way!

Update 1


Here I am basically trying to open various files and read chars from them

int main(int argc, char * argv[])
{
    int i = 1;
    FILE * fp = NULL;

    if(1==argc){
    do_read(stdin);
    }else{
    // cycle through all files in command line arguments and read them
        for (i=1; i < argc; i++) {
            if ((fp = fopen(argv[i], "r")) == NULL) {
                printf("Failed to open file.\n");
            }
            else {
                do_read(fp);
                fclose(fp);

            }
        }
    }

    //printf("\n");

    //printf("\n");
    printf("%i",charCount);
    return 0;
}
Cesar A
  • 663
  • 1
  • 7
  • 10

3 Answers3

2

sample

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

void proc_rev(char toReverse[], char outputArray[]){
    int charCount = strlen(toReverse);
    int i;
    int tempCharCount = charCount;
    int wordSize = 0;
    int outputIndex = 0;
    int sentenceIndex = 0;
    int charStep = 0;
    for(i = charCount-1; i>=0; i--){
        if(toReverse[i] == ' '){
            int j;
            sentenceIndex = tempCharCount - wordSize;
            for(j = 0; j<charStep; j++){
                outputArray[outputIndex++] = toReverse[sentenceIndex++];
            }
            outputArray[outputIndex] = ' ';
            outputIndex++;
            charStep = 0;
        }
        wordSize++;
        charStep++;
    }
    outputArray[outputIndex] = '\0';
}

int main(void){
    FILE *fp = stdin;

    char toReverse[1000] = " ";
    char outputArray[1000];

    while(1 == fscanf(fp, "%998[^\n]%*c", &toReverse[1])){
        proc_rev(toReverse, outputArray);
        puts(outputArray);
    }

    return 0;
}

void do_read(FILE *fp){
    char toReverse[1000] = " ";
    char outputArray[1000];

    while(1 == fscanf(fp, "%998[^\n]%*c", &toReverse[1])){
        proc_rev(toReverse, outputArray);
        puts(outputArray);
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • How would I print it? – Cesar A May 05 '15 at 01:16
  • Also, I am trying to read chars from a file... and I would like to use say, the feed from stdin if the command `myprog < file1` is given, and the `fopen` and `fclose` functions to read from files if the program is called such as `myprog file1 file2`. Could you help me achieve this? See my update for more code – Cesar A May 05 '15 at 01:20
  • By the way, my do_read(fp) function would be the equivalent to your proc_rev fun – Cesar A May 05 '15 at 01:23
  • I think that there is no problem. – BLUEPIXY May 05 '15 at 01:27
  • How would you set it up though? Could you explain the while() condition? how come you are using pointers? When can I use pointers? – Cesar A May 05 '15 at 01:30
  • I add sample of `do_read`. `1 == fscanf(fp, "%998[^\n]%*c", &toReverse[1])` Read one line from file. We can use pointers. (e.g FILE *fp) – BLUEPIXY May 05 '15 at 01:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76943/discussion-between-cesar-a-and-bluepixy). – Cesar A May 05 '15 at 01:36
1

This code is not tested but basic idea is reversing the whole string once and then reverse it word by word. idea is correct, implementation may have bugs

void swap(char* s, int i, int j) {
    char tmp = s[i];
    s[i] = s[j];
    s[j] = tmp;
}


void rev(char* stirng, int start, int len) {
    for (int i=0; i<len/2; ++i) {
       swap(string, i, len-i-1);
    }
}

int main() {
    char* string = read from file
    int len = strlen(string);
    rev(string, 0, len);

    for (int i=0; i<len;) {
        int l = 0;
        int j=i;
        while (j<len && string[j]!=' ') ++j;
        rev(string, i, j-i);
        i=j+1;
    }
}
Mohammad Jafar Mashhadi
  • 4,102
  • 3
  • 29
  • 49
0

The current logic reverses individual words from last to second word. However to reverse the first word you will have to add a check apart from if(toReverse[i] == ' ') as the first character need not be a space. A separate check can be used when counter 'i' reaches zero, i.e. first character

Akshay
  • 36
  • 6