String inversion & Word reversal-TV

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.