一道C++编程题
//#include "stdafx.h"//If the vc++6.0, with this line.
#include <iostream>
using namespace std;
char *fun(char *str){
int i,j;
for(j=0;str[j];j++);
--j = j&1 ? j : j-1;
for(i=1;i<j;i+=2,j-=2)
str[i]^=str[j],str[j]^=str[i],str[i]^=str[j];
return str;
}
int main(int argc,char *argv[]){
char s[]="ggoi romndno";
cout << fun(s) << endl;
return 0;
}
运行结果:
#include <stdio.h>
int main()
{int i,j;
char c,s[200];
gets(s);
for(j=0;s[j];j++);
if(j%2==0)j--;
for(i=1;i<j;i+=2,j-=2)
{c=s[i]; s[i]=s[j]; s[j]=c;}
printf("%s\n",s);
return 0;
}
大佬又碰见你了😂
#include <iostream>
using namespace std;
int search(int* p1, int n, int* p2);
void print(int* p, int n);
int main()
{
int arr1[10] = {1,2,3,4,5,6,7,8,9,10};
int arr2[] = { 0 };
int count = search(arr1, 10, arr2);
print(arr1, 10);
print(arr2, count);
system("pause");
}
int search(int* p1, int n, int* p2)
{
int result = 0;
for (int i = 0; i < n; i++)
{
if (p1[i] % 2 != 0)
{
p2[result] = p1[i];
result++;
}
}
return result;
}
void print(int* p, int n)
{
for (int i = 0; i < n; i++)
{
cout << p[i];
}
cout << endl;
}
看了接口有返回值,就不敢直接动输入
用了复制和内存释放,然而我想多了...
#include <string.h>
char *fun(char *str){
int i,len=strlen(str);
char *text=(char*)malloc(sizeof(char)*(len+1));
text[len]='\0';
if(len%2==1){
len--;text[len]=str[len];
}
for(i=0;i<len;i++)
text[i]=i%2==0? str[i] : str[len-i];
return text;
};
int main(){
char str[]="ggoi romndno";
char *text=fun(str);
printf("%s => %s",str,text);
free(text);
return 0;
}
ggoi romndno => good morning
Process returned 0 (0x0) execution time : 0.019 s
Press any key to continue.
能不能先把眼前问题解决了😂