编写函数fun,通过指针实现将一个字符串反向。要求主函数输入字符串,通过调用函数fun实现输入字符串反向

2024-11-07 16:51:32
推荐回答(3个)
回答1:

#include
#include
#include

void fun(char* str)
{
char *begin = str;
char *end = str + strlen(str);
char *front, *back, tmp;

for( front=begin, back=(end-1); front {
tmp = *front;
*front = *back;
*back = tmp;
}
}

void main()
{
char str[200];

printf("请输入字串闷带: ");
gets( str );

fun( str );
printf("反辩雀转后蚂灶芦: %s\n", str );
}

回答2:

12345abcde
edcba54321
Press any key to continue
#include <腔凳stdio.h>
#include "string.h"
void fun(char *buf)
{
char str[100]="\伍桐旅0";
int i,j=0;
for (i=strlen(buf)-1;i>=0;i--)
str[j++]=buf[i];
printf("%s\n",str);
}
main()
{
char buff[100]="\0";
gets(buff);
fun(buff);
}
有问题请追问 满轮晌意记得采纳

回答3:

#include
using namespace std;
char* fun(char *s){
char* start = s;
char* end = s+strlen(s)-1;
while(start < end){
char tmp = *start;
*start = *end;
*end = tmp;
start++;
end++;
}
return s;
}

int main(){
char* str = "hello world!"弯亩;
fun(str);
cout<档闹弊 return 0;
}