How to Reverse Every Other Word in a String in C

The difficulty

Reverse every other word in an offered string, then return the string. Get rid of any leading or tracking whitespace, while guaranteeing there is precisely one area in between each word. Punctuation marks need to be dealt with as if they belong of the word in this difficulty.

The option in C

Choice 1:

 #include << stddef.h>>.
#include << stdbool.h>>.

void reverse_alternate( const char * string, char * result).
{
bool is_word = incorrect, is_second = incorrect, not_first = incorrect;
for (const char * s = string; * s; ++ s) {
if (* s =='' && & & is_word ){
is_word = incorrect;
is_second =! is_second;.
} else if (* s!='' && & &! is_word ){
if (not_first) * result++ =' ';.
is_word = not_first = real;.
}

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=";
.}

Choice 2:

 #include < string.h >. #include < stdlib.h >.
#include < stdio.h >.
extern char * strdup( const char *);.
char * strrev( char * string).
{

size_t length= strlen( string);
.
for( size_t i= 0; i < length/ 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 * result).
{
fixed const char * const whitespace=" tnrvf ";. * outcome=";. char * copy=
strdup( string) <;.

char * word= strtok( copy, whitespace);. for( int i= 0; word!= NULL; word= strtok (NULL, whitespace), i++) {if( i %2). strrev( word);. outcome+ = sprintf (outcome, "% s% s",( i== 0)? "":" ", word);.
}
totally free( copy);.} 

Choice 3:

 #include < string.h && >. #include < stdlib.h >.
void reverse_alternate
( const char * string, < char * result) {outcome[0]=" ;. size_t length= 0, start= 0, stop = 0, parity= 1, index= 0;. for( size_t i= 0; i . #include < stdlib.h >. #include < string.h >. void reverse_alternate( const char * string, char * result);. void tester (const char * string, char * anticipated);. Test( reverse_alternate, Sample_Tests) {
tester(" Did it

work?"," Did ti work?" );. tester(" I actually hope it 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 length= strlen( string);. char sent [i];. memset( sent,' @', length + 1);. reverse_alternate (string, sent);. cr_assert(! strcmp( sent, anticipated ),.
" < Inaccurate Outcome > n nstring =" % s "n nSubmitted:" % s "nExpected:" % s "n n",.
string, sent, anticipated.
);.
} [index++].

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: