如何扭转在C ++中给定的句子(字符串)? [英] How to Reverse a given sentence (string) in C++?
问题描述
例如:如果输入被狗喜欢猫
输出 - CATS喜欢狗
认为我只有使用方法:如果其他条件,而与放大器;对循环,数组,字符串和功能。不是字符串函数,指针与放大器;动态内存分配和放大器;结构。
空间需要是相同的例子,以及
我试着做以下,但它不工作,你能帮助吗?
无效revSent(字符STR []){
炭温度[100];
时int k;
为(ⅰ= sentenceSize;我大于0;我 - )
对于(K = 0; K< sentenceSize; k ++)
温度[K] = STR [I];对于(K = 0; K< sentenceSize; k ++)
如果(温度[K]!='')
为(ⅰ= K; I&≠0;我 - )
的printf(%C,临时[I]);}
这很容易做到这一点在地方,没有任何额外的数据结构:
-
扭转整个字符串:
狗喜欢猫
- >STAC EKIL SGOD
-
反转字符串中的每个单词:
STAC EKIL SGOD
- >猫喜欢
DOGS
提示:你可以使用相同的函数为(1)和(2)
Example: if the input was DOGS LIKE CATS output- CATS LIKE DOGS
consider that I have to use only : If-else conditions, while & for loops, Arrays, strings and Functions. NOT strings functions, Pointers & Dynamic memory allocation & structures. Spaces need to be the same as the example as well.
I tried to do the following but it doesnt work can you help please?
void revSent(char str[]){
char temp[100];
int k;
for (i=sentenceSize ; i>0 ; i--)
for (k=0 ; k<sentenceSize ; k++)
temp[k]=str[i];
for (k=0 ; k<sentenceSize ; k++)
if (temp[k]!=' ')
for (i=k ; i>0 ; i--)
printf("%c", temp[i]);
}
It's easy to do this in-place, without any additional data structures:
reverse the whole string:
DOGS LIKE CATS
->STAC EKIL SGOD
reverse each word in the string:
STAC EKIL SGOD
->CATS LIKE DOGS
Hint: you can use the same function for both (1) and (2).
这篇关于如何扭转在C ++中给定的句子(字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!