c++学习2

实现 reverse函数:

void reverse (char *str)
{
    char * end=str;
    char temp;
    while(*end)
    {
        ++end;
    }
    --end;
    cout<<str<<endl;
    cout<<end<<endl;
    while(str<end){
        temp=*str;
        *str++=*end;
        *end--=temp;
    }


}
int main()
{
    char s[]="abcdef";
    char * str=s;
    reverse(str);
    cout<<str<<endl;
}
// reverse string;

1)这种方法改变的是原始的字符串,使用字符串数组

2)如果初始化char *str="abcdef",字符串常量,常量只能读取不能写入:

    char * str="abc";

    str="def";//正确,因为可以改变本身的指向

    str[3]='p';//错误不能修改字符串中的字符

    所以此方法不适合

3)使用字符串数组or字符串常量

    获取用户输入,写入操作,不用使用字符串常量,

4)区别 (存储区域不同)

    字符数组存储在全局数据区或栈区

    字符串常量存储在常量区

 

posted @ 2017-06-16 19:13  hahahaf  阅读(135)  评论(0编辑  收藏  举报