69,369
社区成员
发帖
与我相关
我的任务
分享
void reverse_str(const char * str1, char * str2)
{
if(*str1 !='\0')
{
reverse_str(str1 + 1, str2);
while(*str2 !='\0')
{
str2 ++;
}
* str2 = *str1;
*(str2+1) = '\0';
}
else
{
str2 = '\0';
}
}
Reverse_String(char *str)
{
int p=0;
while(str[p]!='\0') //判断是不是空字符
p++;
while(p)
{p--;
cout<<str[p];//输出
}
}
#include <stdio.h>
int main (void) {
char string[]="hello,world!";
char temp;
int len=0,i,j;
while(string[len]!='\0') {
len++;
}
for(i=0,j=len-1;i<len/2;i++,j--) {
temp=string[i];
string[i]=string[j];
string[j]=temp;
}
printf ("%s\n",string);
return 0;
}
void swap(char &a,char &b){
a = a^b;
b = a^b;
a = a^b;
}
void Reverse_String(char *str){
int len = strlen(str);
for (int i = 0; i < len/2; ++i){
swap(str[i], str[len-1-i]);
}
}
//库函数也不用,额外变量也不用,我还真想不到...
//以前有过一个题目:
//计算1+2+3+...+100的和。
//条件:不用你学过的判断语句,循环语句等。
//如:if...else,while,do...while,for,switch,goto以及三目运算符等。
//我还是没有想到...
#include <iostream>
using namespace std;
void ReverseStr( const char* s1 ,char* s2)
{
*((int*)s2) = 0;
while (*s1)
{
s1++;
*((int*)s2) += 1;
}
s1 -= *((int*)s2);
s2 += *((int*)s2);
*s2-- = '\0';
while (*s1)
{
*s2-- = *s1++;
}
return ;
}
int main()
{
const char* p = "hello";
char pp[100];
ReverseStr(p, pp);
cout << pp << endl;
return 0;
}
void Reverse_String(char *str)
{
if(str[0]=='\0')
{
return;
}
Reverse_String (&str[1]);
putchar(str[0]);
}
/***
*strrev.c - reverse a string in place
*
* Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines _strrev() - reverse a string in place (not including
* '\0' character)
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *_strrev(string) - reverse a string in place
*
*Purpose:
* Reverses the order of characters in the string. The terminating
* null character remains in place.
*
*Entry:
* char *string - string to reverse
*
*Exit:
* returns string - now with reversed characters
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl _strrev (
char * string
)
{
char *start = string;
char *left = string;
char ch;
while (*string++) /* find end of string */
;
string -= 2;
while (left < string)
{
ch = *left;
*left++ = *string;
*string-- = ch;
}
return(start);
}
#include <stdio.h>
void Reverse_String (char *str)
{
if(str[0]=='\0')
{
return;
}
else
{
Reverse_String (&str[1]);
printf("%c",str[0]);
}
}
int main()
{
char s[]="Hello World";
Reverse_String(s);
printf("\n");
return 0;
}