The problem
Reverse each different phrase in a given string, then return the string. Throw away any main or trailing whitespace, whereas making certain there may be precisely one house between every phrase. Punctuation marks needs to be handled as if they’re part of the phrase on this problem.
The answer in C
Possibility 1:
#embrace <stddef.h>
#embrace <stdbool.h>
void reverse_alternate(const char *string, char *consequence)
{
bool is_word = false, is_second = false, not_first = false;
for (const char *s = string; *s; ++s) {
if (*s == ' ' && is_word) {
is_word = false;
is_second = !is_second;
} else if (*s != ' ' && !is_word) {
if (not_first) *consequence++ = ' ';
is_word = not_first = true;
}
if (is_second && is_word) {
size_t i = 0;
for (const char *ss = s; *ss && *ss != ' '; ++ss, ++i)
;
for (size_t j = 0; j < i; ++j)
consequence[j] = s[i - j - 1];
s += i - 1;
consequence += i;
} else if (*s != ' ')
*consequence++ = *s;
}
*consequence = ' ';
}
Possibility 2:
#embrace <string.h>
#embrace <stdlib.h>
#embrace <stdio.h>
extern char *strdup (const char *);
char *strrev (char *string)
{
size_t size = strlen(string);
for (size_t i = 0; i < size / 2; i++) {
char tmp = string[i];
string[i] = string[length - 1 - i];
string[length - 1 - i] = tmp;
}
return string;
}
void reverse_alternate(const char *string, char *consequence)
{
static const char *const whitespace = " tnrvf";
*consequence = ' ';
char *copy = strdup(string);
char *phrase = strtok(copy, whitespace);
for (int i = 0; phrase != NULL; phrase = strtok(NULL, whitespace), i++) {
if (i % 2)
strrev(phrase);
consequence += sprintf(consequence, "%spercents", (i == 0) ? "" : " " , phrase);
}
free(copy);
}
Possibility 3:
#embrace <string.h>
#embrace <stdlib.h>
void reverse_alternate(const char *string, char *consequence) {
consequence[0] = ' ';
size_t size = 0, begin = 0, cease = 0, parity = 1, index = 0;
for(size_t i=0; i<=strlen(string); i++) {
char curr = string[i];
if((curr == ' ' || curr == ' ') && size) {
if(parity) {
for(size_t j = begin; j < cease; j++) {
consequence[index++] = string[j];
}
}
else {
for(size_t j = cease - 1; j >= begin; j--) {
consequence[index++] = string[j];
}
}
consequence[index++] = ' ';
size = 0;
parity ^= 1;
}
if(curr == ' ') {
proceed;
}
if(size == 0) {
begin = i;
cease = i;
}
size++;
cease++;
}
consequence[index - 1] = ' ';
}
Check circumstances to validate our answer
#embrace <criterion/criterion.h>
#embrace <stdlib.h>
#embrace <string.h>
void reverse_alternate(const char *string, char *consequence);
void tester(const char *string, char *anticipated);
Check(reverse_alternate, Sample_Tests) {
tester("Did it work?", "Did ti work?");
tester("I actually hope it really works this time...", "I yllaer hope ti works siht time...");
tester("Reverse this string, please!", "Reverse siht string, !esaelp");
tester("Have a beer", "Have a beer");
tester(" ", "");
}
void tester(const char *string, char *anticipated) {
size_t size = strlen(string);
char submitted[length + 1];
memset(submitted, '@', size + 1);
reverse_alternate(string, submitted);
cr_assert( !strcmp(submitted, anticipated),
"< Incorrect End result >n nstring = "%s"n nSubmitted: "%s"nExpected: "%s"n n",
string, submitted, anticipated
);
}