本文介绍了扭转词串词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够扭转的字符串。例如,我可以扭转反向字符串到esrever一个gnirts。但我不能够通过文字来扭转它像字字符串反向。
无效reverseString(字符字符串[],字符*开始,字符*完)
{ 焦炭TMP; //临时变量来交换值
诠释计数= 0;
同时(开始与LT端)
{
如果(*开始=='')
{
的printf(发现空间计数%d个\\ N算);
reverseString(字符串,开始计数,启动);
}
TMP = *启动;
*开始= *结束;
*结束= tmp目录;
*启动++;
*结束 - ;
算上++;
} 的printf(字符串%S \\ n,字符串);
}诠释的main()
{
字符字符串[] =扭转词串词;
字符*开始=字符串;
字符*年底开始= strlen的+(字符串)-1;
reverseString(字符串,开始,结束);
返回0;
}
解决方案
这是这样的。我能够扭转串字为单位,以及整个字符串。刚刚经过code,看看逻辑有所帮助。
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;无效stringrev(字符*);
无效reverseWords(字符*);
无效reverseString(字符*,INT);诠释的main()
{ 字符字符串[] =扭转词串词;
reverseWords(字符串);
的printf(\\ nWord英明反转字符串:%S \\ n,字符串);
stringrev(字符串);
返回0;}无效reverseWords(字符*海峡)
{
INT I = 0,J = 0;
reverseString(STR,strlen的(STR));
而(1)//死循环
{
如果(*(STR + J)==''|| *(STR + J)=='\\ 0')//找到的单词或达到句子的末尾
{
reverseString(STR + I,J-I);
I = J + 1;
}
如果(*(STR + J)=='\\ 0')
{
打破;
}
J ++;
}
}无效reverseString(的char * str中,INT LEN)
{
INT I,J;
焦温度;
I = J =温度= 0; J = LEN-1;
对于(i = 0; I<焦耳;我++,j--)
{
TEMP = STR [I]
海峡[I] = STR [J]。
海峡[J] =温度;
}
}无效stringrev(字符*海峡)
{
INT I = -1,J = 0;
字符转[50]; 而(STR [我++] ='\\ 0'!); 而(ⅰ> = 0)
转[J ++] = STR [我 - ] 转[J] ='\\ 0'; 的printf(字符串的\\ nComplete相反的情况:%S \\ n,REV);
}
I am able to reverse a string. For example, I can reverse "reverse a string" to "esrever a gnirts". But I am not able to reverse it word by word like "string a reverse".
void reverseString(char string[],char *start, char* end)
{
char tmp; //temporary variable to swap values
int count = 0;
while(start<end)
{
if(*start==' ')
{
printf("found space count %d \n",count);
reverseString(string,start-count,start);
}
tmp = *start;
*start = *end;
*end = tmp;
*start++;
*end--;
count++;
}
printf(" string %s \n", string);
}
int main()
{
char string[] = "reverse a string word by word";
char *start =string;
char *end =start+ strlen(string) -1;
reverseString(string,start,end);
return 0;
}
解决方案
This is the way. I am able to reverse a string word-wise, as well as the entire string. Just go through the code and see if the logic helps.
#include <stdio.h>
#include <string.h>
void stringrev(char *);
void reverseWords(char *);
void reverseString(char* , int);
int main()
{
char string[] = "reverse a string word by word";
reverseWords(string);
printf("\nWord-Wise Reversed String : %s\n",string);
stringrev(string);
return 0;
}
void reverseWords(char * str)
{
int i = 0, j = 0;
reverseString( str, strlen(str) );
while( 1 ) // Loop forever
{
if( *(str+j) == ' ' || *(str+j) == '\0') // Found a word or reached the end of sentence
{
reverseString( str+i, j-i );
i = j+1;
}
if( *(str+j) == '\0')
{
break;
}
j++;
}
}
void reverseString(char* str, int len)
{
int i, j;
char temp;
i=j=temp=0;
j=len-1;
for (i=0; i<j; i++, j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
void stringrev(char *str)
{
int i=-1,j=0;
char rev[50];
while(str[i++]!='\0');
while(i>=0)
rev[j++] = str[i--];
rev[j]='\0';
printf("\nComplete reverse of the string is : %s\n",rev);
}
这篇关于扭转词串词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!