如何使用单个变量反转字符串以进行交换 [英] How do I reverse a string with a single variable for swapping
本文介绍了如何使用单个变量反转字符串以进行交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨伙计!!!
我有一个字符串让我们说'hi"并且只需使用一个
单个变量进行交换来反转它,我该怎么做?
问候,
Rajendra S.
Hi Folks!!!
I have a string let''s say "hi" and got to reverse it using just a
single variable for swapping, how do I do it?
Regards,
Rajendra S.
推荐答案
ra *************** @ gmail.com 写道:
我有一个字符串让我们说' ;喜"只需使用一个
单个变量进行交换就可以反转它,我该怎么办?
I have a string let''s say "hi" and got to reverse it using just a
single variable for swapping, how do I do it?
这是这个功课吗?
>
-
Keith Thompson(The_Other_Keith) ks***@mib.org < ; http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
Is this homework?
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
听起来微不足道
不知道我是否做得对
仅使用1 var''newvar''
#include< stdio.h>
int main()
{
char main [2] =" hi";
char newvar [2] =" 0";
newvar [0] = main [1];
newvar [1] = main [0];
printf ("%s \ nn",newvar);
返回0;
}
ra *************** @ gmail.com 写道:
嗨伙计!!!
我有一个字符串让我们说嗨并且只使用一个
单个变量进行交换来反转它,我该怎么做?
问候,
Rajendra S。
Hi Folks!!!
I have a string let''s say "hi" and got to reverse it using just a
single variable for swapping, how do I do it?
Regards,
Rajendra S.
我想你想要这个,
char * str =" hello" ;;
int i ,j,k;
k = strlen(str)-1;
for(i = 0,j = k; i< k / 2; i ++,j-- ){
str [i] ^ = str [j];
str [j] ^ = str [i];
str [ i] ^ = str [j];
}
puts(str);
I think you want this,
char *str="hello";
int i,j,k;
k=strlen(str)-1;
for (i=0, j=k; i<k/2; i++,j--) {
str[i]^=str[j];
str[j]^=str[i];
str[i]^=str[j];
}
puts (str);
>
这篇关于如何使用单个变量反转字符串以进行交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文