Hello Everyone!
I hate to ask this question as I feel it decrements myself from problem-solving a solution I'm working on. Currently, I've written a program that has the capability of storing a sentence in an char array and then putting them out in reverse. For example:
Enter Sentence: The Quick Brown Fox!
Reversal of sentence: !xoF nworB kciuQ ehT
Nevertheless, my goal is to create the program so that the reversal sentence will instead read "Fox Brown Quick The." Now I've attempted to problem solve through I've read on forums that in order to solve this you need to first reverse the whole string and then individually reverse the words in the array. However, I feel I have yet to reach sensible conclusion.
I'm going to attach two files: the one one that gives the above result as well as an attempted try at switching the arrays in order to output, to what i believe, a logical result. I understand I am missing something key in this solution and therefore I would appreciate the help. I'm not asking for free handouts so I hope I don't anger anyone in asking for a little assistance in this problem.
first attempt
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int main (void)
{
char msg[SIZE], getLetter;
int count = 0, len = 0, output;
printf("Enter Sentence: ");
getLetter = getchar();
while (count < SIZE && getLetter != '\n')
{
msg[count] = getLetter;
count++;
len++;
getLetter = getchar();
}
printf("Reversal of sentence: ");
for (output = len - 1; output >= 0; output--)
{
putchar(msg[output]);
}
printf("\n\n");
system("pause");
return 0;
}
second attempt
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 100
#define HAND 100
int main()
{
char msg[SIZE], temp[HAND], getLetter, holdLetter;
int count = 0, len = 0, output, second_output;
printf("Enter Sentence: ");
getLetter = getchar();
while (getLetter != '\n')
{
msg[count] = getLetter;
count++;
getLetter = getchar();
}
printf("Reversal of sentence: ");
for (output = count - 1; output >= 0; output--)
{
holdLetter = msg[output];
while (holdLetter != '\n')
{
if (holdLetter == ' ')
{
for (second_output = len - 1; second_output >= 0; second_output--)
putchar(temp[second_output]);
}
else
{
temp[len] = holdLetter;
len++;
}
}
}
system("pause");
return 0;
}
Thank You for reading and have a wonderful day!CIS_161.cCIS_161.c