El reto
Invierta todas las demás palabras en una cadena dada, luego devuelva la cadena. Deseche cualquier espacio en blanco inicial o remaining, mientras se asegura de que haya exactamente un espacio entre cada palabra. Los signos de puntuación deben tratarse como si fueran parte de la palabra en este desafío.
La solución en C
Opción 1:
#embody <stddef.h>
#embody <stdbool.h>
void reverse_alternate(const char *string, char *outcome)
{
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) *outcome++ = ' ';
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)
outcome(j) = s(i - j - 1);
s += i - 1;
outcome += i;
} else if (*s != ' ')
*outcome++ = *s;
}
*outcome = ' ';
}
Opcion 2:
#embody <string.h>
#embody <stdlib.h>
#embody <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(size - 1 - i);
string(size - 1 - i) = tmp;
}
return string;
}
void reverse_alternate(const char *string, char *outcome)
{
static const char *const whitespace = " tnrvf";
*outcome = ' ';
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);
outcome += sprintf(outcome, "%spercents", (i == 0) ? "" : " " , phrase);
}
free(copy);
}
Opción 3:
#embody <string.h>
#embody <stdlib.h>
void reverse_alternate(const char *string, char *outcome) {
outcome(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++) {
outcome(index++) = string(j);
}
}
else {
for(size_t j = cease - 1; j >= begin; j--) {
outcome(index++) = string(j);
}
}
outcome(index++) = ' ';
size = 0;
parity ^= 1;
}
if(curr == ' ') {
proceed;
}
if(size == 0) {
begin = i;
cease = i;
}
size++;
cease++;
}
outcome(index - 1) = ' ';
}
Casos de prueba para validar nuestra solución
#embody <criterion/criterion.h>
#embody <stdlib.h>
#embody <string.h>
void reverse_alternate(const char *string, char *outcome);
void tester(const char *string, char *anticipated);
Take a look at(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(size + 1);
memset(submitted, '@', size + 1);
reverse_alternate(string, submitted);
cr_assert( !strcmp(submitted, anticipated),
"< Incorrect Consequence >n nstring = "%s"n nSubmitted: "%s"nExpected: "%s"n n",
string, submitted, anticipated
);
}