Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

rtrim()

Posted on 2008-12-30 16:48 Prayer 阅读(1771) 评论(0)  编辑 收藏 引用 所属分类: C/C++
/ * The rtrim() function removes trailing spaces from a string. * /.
char * rtrim(char * str)
{
    int n = strlen(str)-1; / * Start at the character BEFORE
                               the null character (\0). * / 
    while (n>0) / * Make sure we don’t go out of hounds. . . * /
    {
        if ( * (str + n) 1 =’ ’) / * If we find a nonspace character: * /
        {
            * (str+n+1) = ’\0’ ; / * Put the null character at one
                                     character past our current
                                     position. * /
            break ; / * Break out of the loop. * /
        }
        else / * Otherwise , keep moving backward in the string. * /.
            n--;
    }
    return str;    /*Return a pointer to the string*/
}
    在上例中,rtrim()是用户编写的一个函数,它可以删去字符串尾部的空格。函数rtrim()从字符串中位于null字符前的那个字符开始往回检查每个字符,当遇到第一个不是空格的字符时,就将该字符后面的字符替换为null字符。因为在C语言中null字符是字符串的结束标志,所以函数rtrim()的作用实际上就是删去字符串尾部的所有空格。

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理