20161126

来源:互联网 发布:淘宝秒杀器 编辑:程序博客网 时间:2024/06/17 12:16

做类似于从“i am from nanjin”到“nanjin from am i”的逆序句子。


include <stdio.h>

#include <string.h>


#define MAX_SIZE 1024

void reverse_word(char *src)
{
    int word_len = 0;


    while(*src != '\0')
    {
        if(*src == ' ')
{
            reverse_string(src - word_len,word_len);
   word_len = 0;
}
else
{
            word_len++;
}


src++;
    }


    reverse_string(src - word_len, word_len);

}

void reverse_string(char *s1, int len)
{
    int i;


    char temp;


    for(i = 0; i < len / 2; i++)
    {
        temp = *(s1 + i);
*(s1 + i) = *(s1 + len - 1 - i);
*(s1 + len - 1 - i) = temp;
    }
}

int main()
{
    char src[MAX_SIZE];


    printf("Please input sentence:\n");
    gets(src);


    reverse_string(src,strlen(src));
    
    reverse_word(src);
    
    printf("reverse:%s\n",src);


    return 0;

}

当有很多键入读取输出语句时若运行时无法进行一个数据类型的键入要加getchar()

将换行从内存中拿走。

或者在scanf中格式前加空格或%*c


计算机由cpu 内存和硬盘组成

程序运行时内存会有一个4g虚拟空间生成最高地址的1G空间为内核空间,剩下的3G为用户空间,又分为栈空间,堆空间,数据区和代码段,

局部变量,函数形参,自动变量在栈空间存储,malloc,ralloc,calloc在堆空间,数据区分为三种:1,bss:未初始化全局变量 2,or:常量(只读)

3,静态数据区:static修饰的变量,初始化的全局变量。代码段放代码。

栈特点为1,先进后出 2,系统管理。生命周期短

堆特点为1,先进先出 2,用户管理。

未初始化局部变量,输出为默认垃圾值

未初始化全局变量,输出为默认为0.

重名时,局部变量起作用

分.c写程序时若显示函数内部变量未定义,在include下面加上extern int  a

声明在外部定义整型变量a

0 0
原创粉丝点击