Question #47384

Write an algorithm that inverse a line of text without inversing the word.
1

Expert's answer

2014-10-09T01:22:02-0400

Here is the processed document with the code block restored and formatted:

Problem.

Write an algorithm that inverse a line of text without inversing the word.

Solution.

#include <stdio.h>
#include <string.h>
int main() {
    char input[256];
    char output[256];
    int iInput;
    int iOutput;
    int j;
    int k;
    int len;
    gets(input);
    strcpy(output, input);
    len = strlen(input);
    iInput = len;
    iOutput = 0;
    // String reversing
    while (iInput != 0) {
        j = iInput - 1;
        while ((input[j] != ' ') && (j != 0)) {
            j--;
        }
        if (j == 0) {
            for (k = j; k <= iInput; k++) {
                output[iOutput] = input[k];
                iOutput++;
            }
            iInput = 0;
        } else if (input[j] == ' ') {
            for (k = j + 1; k < iInput; k++) {
                output[iOutput] = input[k];
                iOutput++;
            }
            output[iOutput] = ' ';
            iOutput++;
            iInput = j;
        }
    }
    printf("%s", output);
    return 0;
}

Result

this is line of text

text of line is this

http://www.AssignmentExpert.com/</string.h></stdio.h>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS