zoukankan      html  css  js  c++  java
  • 字符串操作

    字符串操作讲解
    strlen原型:int strlen(const char *str)
        功能:返回字符串的实际长度,不含’\0’
    strcpy原型:char *strcpy(char *dest,const char *src)
        功能:把src所指向的以\0结尾的字符串复制到dest所指向的数组中(含n,代表只拷贝前n个)
    char *strcmp(char *str1,char *str2)
    char *strncmp(char *str1,char *str2,int n)
    功能:比较str1和str2的大小,相等返回0,
        str1大于str2返回大于0,str1小于str2返回小于0(n代表只比较前n个)
        char *strcat(char *str1,char *str2)
        char *strncat(char *str1,char *str2,int n)
        功能:将字str2连接到str1后面(n代表连接指定位数)
    strchr //字符串中字符首次匹配函数
    strrchr //字符串中字符末次匹配函数
    strstr //字符串匹配函数
    strtok //字符串分割函数
    memchr //字符搜索(不限定在字符串中搜索)
    strset //字符串设定函数
    memset //空间设定函数
    atoi/atol/atof //字符串转换功能

    字符串练习
    《计算器练习》

    1、熟悉字符串处理函数
        strlen();
        strcpy();
        strncpy();
        strcat();
        strncat();
        strcmp();
        strncmp();

    2、使用(strncpy,strncat等字符串处理函数)编写一个函数,作用是把一个char组成的字符串循环右移n个。
    比如原来是p="abcdefghi”如果n=2,移位后应该是“hiabcdefg”

    3、使用strstr()字符串匹配函数,在已给字符串1中查找字符串2,打印输出其值,并统计其出现次数
       如:char str1[] = “abcdebchbckl”
           char *str2 = “bc”,统计bc出现的位置及次数,打印输出
        
    4、使用memchr()函数,将上题str1中的b、c全部用$替代

    5_1、参考书上的strtok函数,
    将char name[100]={"13466630259,20091023,今天天气挺好"}
    切割为三个字符串,并在屏幕上打印输出:来电....,时间....,内容...

    5_2、以下为我们的手机收到的短信的格式,请利用指针数组与strtok函数对其解析
        char msg_src[300]={"+CMGR: \"REC UNREAD\", \"+8613466630259\",
                            \"98/10/01,18 :22 :11+00\",ABCdefGHI"};

        msg_deal(char *msg_src, char *msg_done[])

    6、函数指针练习
    设计一个计算器,可以进行加、减、乘、除运算。
    要求从键盘输入指令及操作数,如:
    add 15 25        //计算15+25的值,并返回结果
    sub 25 15        //计算25-15的值,并返回结果
    mux 3  5        //计算 3*5的值,并返回结果
    div 25 5         //计算25/5的值,并返回结果
    要求可以循环输入。
    1. 用字符串比较函数strcmp
    2. 将命令有两种方案:
       a  scanf("%s %d %d",name,%d,%d);
          优点:程序简单 缺点:参数一旦输错,程序就会死了
       
       b  使用gets(buf)将所有的输入放入一个缓冲区,
    然后利用strtok对多buf进行分析处理。buf[100]=“add 15 25”
          优点:可以对输入的数进行有效性检测
          缺点:程序比较复杂

    3. 可考虑采用函数指针。
        采用函数指针或回调函数的形式,对各功能函数调用

    习题答案
    2、
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>

    typedef unsigned char uchar ;
    typedef unsigned int uint ;

    char str[] = "0123456789abcdaef";
    //使用字符串处理函数,编写一个函数,作用是把一个char组成的字符串循环右移n个。


    void fun(char *str)
    {
        char *pr;
        uchar n;
        printf("循环右移前的字符串:%s\n", str);
        printf("请输入循环右移位数:");
        scanf("%d", &n);
        n%=strlen(str);
         pr=(char *)calloc(sizeof(char),strlen(str)+1);
        strncpy(pr, str+strlen(str)-n, n);
        strncpy(pr+n, str, strlen(str)-n);
        *(pr+strlen(str))='\0';
        printf("\n循环右移后的字符串:%s\n", pr );
        free(pr);
    }


    int main(void)
    {
        uchar ch;
        while(1)
        {
            fun(str);
            ch=getch();
            if(ch==27)
            {
                break;
            }
            else
            {
                system("cls");
            }
        }
        return 0;
    }

    3、
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>

    typedef unsigned char uchar ;
    typedef unsigned int uint ;

    char str1[] = "abcdebchbcklasfadfiojjbcjhjkljkljbcjjklkljbcjklkibc";
    char str2[] = "bc";

    /*
        使用strstr()字符串匹配函数,在已给字符串1中查找字符串2,打印输出其值,并统计其出现次数
        如:char str1[] = “abcdebchbckl”
            char *str2 = “bc”,统计bc出现的位置及次数,打印输出
    */


    void fun(char *str1, char *str2)
    {
        char *temp;
        uint times = 0;
        temp = str1;
        while(strstr(temp, str2)!=0)
        {
            temp = strstr(temp, str2)+strlen(str2);
            times++;
            printf("%s第%d次出现的位置:%d\n", str2, times, temp-str1);
        }
        printf("%s总共出现的次数:%d\n", str2, times);
    }


    int main(void)
    {
        fun(str1, str2);
        return 0;
    }

    4、
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>

    typedef unsigned char uchar ;
    typedef unsigned int uint ;

    char str1[] = "abcdebchbcklasfadfiojjbcjhjkljkljbcjjklkljbcjklkibc";

    /*
        使用memchr()函数,将上题str1中的b、c全部用$替代
    */

    void tihuan(char *str, char ch)
    {
        char *temp;
        temp = str;
        while(memchr(temp, ch, strlen(temp)))
        {
             temp = memchr(temp, ch, strlen(temp));
            *temp = '$';
            temp++;
        }
    }
    void fun(char *str)
    {
        printf("替换前的字符串:%s\n", str);
        tihuan(str, 'b');
        tihuan(str, 'c');
        printf("替换后的字符串:%s\n", str);
    }

    int main(void)
    {
        fun(str1);
        return 0;
    }

    5、
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>

    typedef unsigned char uchar ;
    typedef unsigned int uint ;

    char name[100]={"13466630259,20091023,今天天气挺好"};
    char split[]=",";
    /*
        参考书上的strtok函数,
        将char name[100]={"13466630259,20091023,今天天气挺好"}
        切割为三个字符串,并在屏幕上打印输出:来电....,时间....,内容...
    */


    void fun(char *str, char *split)
    {
        char *tel, *time, *content;
        tel = strtok(str, split);
        printf("来电:%s\n", tel);
        time = strtok(NULL, split);
        printf("时间:%s\n", time);
        content = strtok(NULL, split);
        printf("内容:%s\n", content);
    }

    int main(void)
    {
        fun(name, split);
        return 0;
    }

    6、
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>

    typedef unsigned char uchar ;
    typedef unsigned int uint ;
    /*
    设计一个计算器,可以进行加、减、乘、除运算。
    要求从键盘输入指令及操作数,如:
    add 15 25        //计算15+25的值,并返回结果
    sub 25 15        //计算25-15的值,并返回结果
    mux 3  5        //计算 3*5的值,并返回结果
    div 25 5         //计算25/5的值,并返回结果
    要求可以循环输入。
    1. 用字符串比较函数strcmp
    2. 将命令有两种方案:
       a  scanf("%s %d %d",name,%d,%d);
          优点:程序简单 缺点:参数一旦输错,程序就会死了
       
       b  使用gets(buf)将所有的输入放入一个缓冲区,
    然后利用strtok对多buf进行分析处理。buf[100]=“add 15 25”
          优点:可以对输入的数进行有效性检测
          缺点:程序比较复杂

    3. 可考虑采用函数指针。
        采用函数指针或回调函数的形式,对各功能函数调用
    */
    int fun_add(int a, int b)
    {
        return (a+b);
    }

    int fun_sub(int a, int b)
    {
        return (a-b);
    }

    int fun_mux(int a, int b)
    {
        return (a*b);
    }

    int fun_div(int a, int b)
    {
        return (a/b);
    }

    uint conver(char *str)
    {
        uint lenth;
        uint i;
        uint value = 0;

        lenth = strlen(str);
        for(i=0;i<lenth;i++)
        {
            value *= 10;
            value += *(str+i)-'0';
        }
        return value;
    }

    void fun(char *str, char *split)
    {
        char *opearationpr, *data1pr, *data2pr;
        int (*function)(int, int);
        int result;

        opearationpr = strtok(str, split);
        data1pr = strtok(NULL, split);
        data2pr = strtok(NULL, split);
        if(    opearationpr != NULL && data1pr !=NULL && data2pr !=NULL
            && (strcmp(opearationpr, "add") == 0
            || strcmp(opearationpr, "sub") == 0
            || strcmp(opearationpr, "mux") == 0
            || strcmp(opearationpr, "div") == 0) )
        {
                if(strcmp(opearationpr, "add") == 0  )
                {
                    function = fun_add;
                }
                else if(strcmp(opearationpr, "sub") == 0)
                {
                    function = fun_sub;
                }
                else if(strcmp(opearationpr, "mux") == 0)
                {
                    function = fun_mux;
                }
                else if(strcmp(opearationpr, "div") == 0)
                {
                    function = fun_div;
                }
                result = function(conver(data1pr), conver(data2pr));
                printf("\n计算结果:%d\n", result);
        }
        else
        {
            printf("\n输入格式有误!\n");
        }
        printf("按空格重新开始\n");
    }

    void calculate(void)
    {
        //输入为字母或数字时分配内存
        //前3个为操作方式说明,数字之间用空格分开
        uchar ch,buffer[50], i = 0;
        printf("              简易计算器             \n");
        printf("\n输入范例:\n");
        printf("add 15 25 //计算15+25的值,并返回结果\n");
        printf("sub 25 15 //计算25-15的值,并返回结果\n");
        printf("mux 3  5  //计算 3*5 的值,并返回结果\n");
        printf("div 25 5  //计算25/5 的值,并返回结果\n");
        printf("请输入:\n");
        while((ch=getch())!=13)
        {
            if(ch == 8)
            {
                printf("\b \b");
                buffer[i--] = '\0';
            }
            else
            {
                putchar(ch);
                buffer[i++] = ch;
            }
            if(i==49)
            {
                break;
            }
        }
        buffer[i] = '\0';
        fun(buffer, " ");
    }

    int main(void)
    {
        while(1)
        {
            calculate();
            while(getch()!=' ');
            system("cls");
        }
        return 0;
    }


           

  • 相关阅读:
    Java数组的使用
    Java的栈堆以及数组两种不同类型的定义
    Java数组声明的创建
    JAVA递归
    Java可变参数
    Java方法(类--------对象--------方法)
    html块元素和内联元素的区别
    HTML基础介绍
    CSS网页美化元素属性介绍
    ArrayList类的remove(Object o)方法简述
  • 原文地址:https://www.cnblogs.com/qinkai/p/2429590.html
Copyright © 2011-2022 走看看