The practice program of C on point

//字符反向排列
//vision 1.2
#include<stdio.h>

void reverse_string( char *str )
{
    char *string;//第一个字符位置
    char *last_char;//最后一个字符位置

    //for( last_char = str; ; last_char++ )
       // if( *last_char == '\0' )
         //   break;
    // for( last_char = str; *last_char != '\0'; last_char++ )
     // ;

    /*
    **设置last_char存储最后一个字符位置
    */
    for( last_char = &str[0]; *last_char != '\0'; last_char++ )
     ;

    string = &str[0];
    last_char--;

    /*
    **交换前后指针指向位置的值  前指针增加  后指针减少
    */
    while( string < last_char )
    {
        char temp;
        temp = *string;
        *string++ = *last_char;
        *last_char-- = temp;
    }

    printf( "%s", str );
}

int main( void )
{
    char str[10] = {"abcdef"};
    reverse_string(str);
    return 0;
}

在不使用库函数情况下 自己还是把这个库函数里有的函数敲出来了。只是有个迷惑的地方,这是终究版本,但之前有个版本,输出始终只有源字符串的一半长度,同时也是反转了的,不理解。希望有明白的朋友看见后可以告知下。小子我在此谢谢了~源码如下:

//字符反向排列
//vision 1.2
//程序结果错误 为什么?
//就目前  我猜测问题出在数组的指针上
//直接对原数组的指针进行操作 应该找个中间量存储开始指针。
//未完待续····
#include<stdio.h>

void reverse_string( char *str )
{
    char *last_char;//最后一个字符位置

    //for( last_char = str; ; last_char++ )
       // if( *last_char == '\0' )
         //   break;
    // for( last_char = str; *last_char != '\0'; last_char++ )
     // ;

    /*
    **设置last_char存储最后一个字符位置
    */
    for( last_char = &str[0]; *last_char != '\0'; last_char++ )
     ;

    last_char--;

    /*
    **交换前后指针指向位置的值  前指针增加  后指针减少
    */
    while( str < last_char )
    {
        char temp;
        temp = *str;
        *str++ = *last_char;
        *last_char-- = temp;
    }

    printf( "%s", str );
}

int main( void )
{
    char str[10] = {"abcdxuf"};
    reverse_string(str);
    return 0;
}

只是多了个中间变量而已,这是为什么呢?

时间: 2024-10-10 15:00:40

The practice program of C on point的相关文章

Book Review of “The practice of programming” (Ⅳ)

The practice of programming Chapter 4 Interfaces A good programmer should always be good at designing. The essence of design is to balance competing goals and constraints. When we do programming, we need to design a friendly, portable and flexible in

SDN实战: Build a mini-lab environment and practice SDN-IP/ONOS with GNS3, Mininet and VMware

SDN IN ACTION: Build a mini-lab environment and practice SDN-IP/ONOS with GNS3, Mininet and VMware    薛国锋  [email protected] 本文主要通过简单的实验,对SDN相关概念以及ONOS开源软件等建立一个感性的印象,加深对核心概念的理解. SDN-IP is to adopt SDN/OpenFlow switches to replace the traditional IP/M

汉企C#面向对象——继承Practice

class Dianqi //创建电器类:父类 { private string _Dianqimingzi; public string Dianqimingzi { get { return _Dianqimingzi; } set { _Dianqimingzi = value; } } } class Computer:Dianqi //创建电脑类:子类 { private string _Diannaomingzi; public string Diannaomingzi { get

Don’t Nail Your Program into the Upright Position

Don't Nail Your Program into the Upright Position Verity Stob i ONCE WROTE A SPOOF C++ qUiz, in which I satirically suggested the fol- lowing strategy for exception handling: By dint of plentiful try-catch constructs throughout our codebase, we are s

文摘:Slopegraphs for comparing gradients: Slopegraph theory and practice

原文地址:https://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0003nk Slopegraphs for comparing gradients: Slopegraph theory and practice Slopegraphs compare changes usually over time for a listof nouns located on an ordinal or interval scale. Many

95-712 Practice problem

95-712 Practice problem 8This problem brings together what you learned in inheritance,polymorphism, access specifiers, and JavaFXProblem statement: The GUI for this app is provided to you(Fig.1). It currently supports three pets: Cat, Dog, and Bird.U

error: stray &#39;\377&#39; in program

cygwin编译报错:**.cpp:1:1: error: stray '\377' in program解决方法 2014-06-12 00:41 2061人阅读 评论(1) 收藏 举报 编译报错内容: [armeabi] Compile++ thumb: cocos2dcpp_shared <= HelloWorldScene.cpp jni/../../Classes/HelloWorldScene.cpp:1:1: error: stray '\377' in programjni/..

hdu 1094 A+B for Input-Output Practice (VI)

A+B for Input-Output Practice (VI) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 63087    Accepted Submission(s): 42387 Problem Description Your task is to calculate the sum of some integers.

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i