I am attempting to reverse a string for a homework assignment and then reverse each word in the string. The trick is we are suppose to code it without using functions. Like "I like cats" should be 1st "stac ekil I" and then "cats like I". Here is what I have so far:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[40], temp, start, last, count;
int len, l, r, x;
printf("Please input a sentence (under 40 characters) followed by a period.\n");
gets(str);
len = strlen(str);
l = 0;
r = len - 1;
while (l < r) // reverses the whole string
{
temp=str[l];
str[l++] = str[r];
str[r--] = temp;
}
/*for (x = 0; x <=len - 1; x++)
printf("%c", str[x]);*/
count = 0;
start = 0;
for (x = 0; x <= len -1; x++) // reverses each word
{
++count;
if (str[x] == ' ')
last = x - 1;
start = last - count + 1;
while (start < last)
{
char temp;
temp=str[start];
str[start++] = str[last];
str[last--] = temp;
}
count=0;
}
for (x = 0; x <=len - 1; x++)
printf("%c", str[x]);
return 0;
}