首页 > 试题广场 >

使用CC++语言写一个函数,实现字符串的反转,要求不能用任

[问答题]
使用C/C++语言写一个函数,实现字符串的反转,要求不能用任何系统函数,且时间复杂度最小。
函数原型是:char *reverse_str(char *str)
推荐
/*实现字符串翻转*/
char* reverse_str(char* str)
{
    if(NULL == str) //字符串为空直接返回
    {
        return str;
    }
    char *begin;
    char *end;
    begin = end = str;
    while(*end != '\0') //end指向字符串的末尾
    {
        end++;
    }
    --end;
    char temp;
    while(begin < end) //交换两个字符 
    { 
        temp = *begin; 
        *begin = *end; 
        *end = temp; 
        begin++; 
        end--; 
    } 
    return str; //返回结果 
} 
void main() 
{ 
    char str[] = "123456"; 
    printf(reverse_str(str)); 
}
编辑于 2015-02-09 17:42:25 回复(1)
char *reverse_str(char *str){
    char *p1 = str, *p2 = str;
    while (*p2) p2++;p2--;
    for (; p1 < p2; p1++, p2--){
        char t = *p1;
        *p1 = *p2;
        *p2 = t;
    }
    return str;
}

发表于 2017-03-24 20:26:36 回复(0)
如果数据结构不算系统函数,提供一个最直观的思路,但不代表最优
#include <iostream>
#include <stack>

char *reverse_str(char *str){
	if (str==NULL)
	{
		return str;
	}
	int lens = 0;
	std::stack<char> stk;
	while (*str != '\0'){
		stk.push(*str);
		++lens;
		++str;
	}

	char *dest_org = (char*)malloc(lens + 1);
	char *dest = dest_org;
#if 1
	for (size_t i = 0; i < lens; i++)
	{
		*dest = stk.top();
		stk.pop();
		++dest;
	}
#else
	while (!stk.empty()){
		*dest = stk.top();
		stk.pop();
		++dest;
	}
#endif
	*dest = '\0';
	return dest_org;
}

int main(){
	char str[] = "1234567";
	char *dest = reverse_str(str);
	printf("%s\n", dest);
	system("pause");
	return 0;
}

发表于 2017-02-14 15:19:35 回复(0)
char *reverse_str(char *str){
	int len = 0;
	char *temp = str;
	while(*temp){ 
		++temp ; 
		++len;
	}
	for(int i = 0;i < len / 2;++i){
		*(str + i) ^= *(str + len - i - 1);
	 	*(str + len - i - 1)^=*(str + i) ;
	 	*(str + i) ^= *(str + len - i - 1);
	}
	return str;
} 

发表于 2016-09-14 15:50:27 回复(0)
char *reverse_str(char *str){
    int n=sizeof(str);  //sizeof是运算符,不是系统函数,所有可以使用
    char ch;
    for(int i=0;i<n/2;i++){
    ch=str[i];
    str[n-i-1]=str[i];
    str[n-i-1]=ch;
    }
    return str;
}
发表于 2016-03-22 16:17:39 回复(1)
char *reverse_str(char *str)
{
char *p=NULL;
p=str;
int count=0;
int i=0;
int j=0;
while(*p!='\0')
{
count++;
p++;
}
char []res=new char[count+1];
p=str;
for(i=0;i<count;i++)
{
res[count-1-i]=*(p+i); }
res[count]='\0';
return res; }

发表于 2015-10-05 11:05:53 回复(0)
public class Main {
	public static void main(String[] args) {
		String str = "abc";
		System.out.println(reverse(str));
	}

	public static String reverse(String str) {
		char[] seq = str.toCharArray();
		int pre = 0;
		int end = str.length() - 1;
		while (pre != end) {
			char temp = seq[pre];
			seq[pre] = seq[end];
			seq[end] = temp;
			pre++;
			end--;
		}
		return new String(seq);
	}
}


编辑于 2015-09-14 10:12:38 回复(0)
<div> <span style="color:#333333;">void swap(char *a,char *b)</span> </div> <div> <span style="color:#333333;">{</span> </div> <div> <span style="color:#333333;">char temp = *a;</span> </div> <div> <span style="color:#333333;">*a = *b;</span> </div> <div> <span style="color:#333333;">*b = temp;</span> </div> <div> <span style="color:#333333;">}</span> </div> <div> <span style="color:#333333;"><br /> </span> </div> <div> <span style="color:#333333;">char *reverse_str(char *str)</span> </div> <div> <span style="color:#333333;">{</span> </div> <div> <span style="color:#333333;">int n = 0;</span> </div> <div> <span style="color:#333333;">while(str[n] != '\0')</span> </div> <div> <span style="color:#333333;">n++;</span> </div> <div> <span>for(int i = 0,j = n - 1;i&lt;j;i++,j--)</span> </div> <div> <span>swap(str + i,str + j);</span> </div> <div> <span>return str;</span> </div> <div> <span>}</span> </div> <div> <span><br /> </span> </div> <div> <span style="color:#333333;"><br /> </span> </div>
发表于 2015-09-09 20:59:19 回复(0)
<div> char *reverse_str(char *str){ </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;char *p = str; </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;while(*p!='\0') </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p++; </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;char *begin=str,*end=--p; </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;while(begin&lt;=end){ </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char tmp = *begin; </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*begin++ = *end; </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*end-- = tmp; </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;} </div> <div> &nbsp;&nbsp;&nbsp;&nbsp;return str; </div> <p> } </p>
发表于 2015-08-30 20:52:39 回复(1)
char *reverse_str(char *str)
{
    if(str==NULL ||  *str=='\0')
        return str;

    char *p=str;
    char *q;
    while(*p!='\0')
        p++;
   q=p-1;
   p=str;
    
  while(p!=q)
 {
      //swap(*p,*q)
      *p=(*p)^(*q);
      *q=(*p)^(*q);
      *p=(*p)^(*q);
       p++;q--;
 }
   return str;
}

编辑于 2015-08-02 21:54:47 回复(0)
// 字符串的反转 O(n)
char *reverse_str(char *str)
{	
	if(NULL == str || '\0' == *str)
	{
		return str;
	}

	char *end = str;
	char *start = str; // 头指针

	while (*end != '\0')
	{
		end++;
	}
	
	// 尾指针
	--end;

	while (start < end)
	{
		char temp = *start;
		*start = *end;
		*end = temp;

		start++;
		end--;
	}

	return str;
}
编辑于 2015-07-30 08:52:33 回复(0)
首位对调,然后前指针往后移动,后指针往前移动,依次对调
发表于 2015-06-19 10:04:00 回复(0)
char *reverse_str(char *str)
{
    if (!str || str[0] == '\0')
    {   
        return str;
    }   

    // find the tail
    int j;
    for (j = 0; str[j + 1] != '\0'; j++);                                                                                                                                       

    int middle = j / 2 + j % 2;
    for (int i = 0; i < middle; i++, j--)
    {   
        char tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
    }   

    return str;
}

发表于 2015-01-31 22:04:58 回复(0)