Method One: First the whole character is reversed, and then the word is reversed with a space as the delimiter.
#include <stdio.h>
#include <string.h>
String_change (char * p, int start, int end)//character reversal
{
int I,len;
char temp;
len = strlen (p);
while (Start<=end)
{
temp = P[start];
P[start] = P[end];
P[end] = temp;
start++;
end--;
}
}
int main (void)
{
Char a[20] = "abc def GHI";
int I, start, end, tag = 0;
printf ("%s\n", a);
String_change (A, 0, strlen (a)-1);
for (i = 0; I <= strlen (a); i++)
{
if ((a[i] = = "| | A[i] = = NULL) && tag = = 1)
{
String_change (A, start, end);
tag = 0;
Continue
}
if (a[i]! = ")
{
Switch (TAG)
{
Case 0:tag = 1; Word start
start = i; Word Start position
Break
Case 1:end = i;
Break
}
}
}
printf ("%s\n", a);
return 0;
}
Method Two: Find where each word starts, and then invert. Reproduced
int Reversewords (char *str,char *resultwords[])
{
int wordstart = 0; /* Determine if the word starts, notice initialization */
int wordCount = 0; /* Count the number of words, pay attention to initialize */
Char *tempwords[256]; /* Pointer array to hold the starting address for each word */
int i; /* loop control variable, traverse raw String */
Int J; /* loop control variable, control the word address */
for (i = 0; Str[i]! = '; i++ ")
{
if (str[i] = = ")//* If the current character is a space, it is not a word */
{
Wordstart = 0; /* Denotes 0 */
Str[i] = ' + '; /* Fill all non-letters as */
}
else/* If the current character is not a space, but the preceding character is a space, the word begins.
if (Wordstart = = 0)
{
Wordstart = 1; /* Start Word */
tempwords[wordcount++] = &str[i]; /* The address of the letter starting with each word */
}
}
for (j = 0; J < WordCount; J + +)
{
RESULTWORDS[J] = tempwords[wordcount-1-j]; /* Reverse, for reverse output */
}
return wordCount; /* Returns the number of words */
}
int main (void)
{
Char str[] = "I love China forever.";
printf ("");
Char *resultwords[256]; /* Pointer array to hold the starting address of the resulting word */
int wordCount = Reversewords (str,resultwords); /* Call */
int i;
for (i = 0; i < WordCount; i++)
{
printf ("%s", Resultwords[i]);
}
Putchar (' \ n ');
return 0;
}
String inversion & Word reversal-TV