• 算法实现c语言--03


    1. 实现  mystrcpy(), mystrcmp(), mystrcat(), mystrlen() ;
      #include<stdio.h>
      #include<stdlib.h>
      
      int mystrlen(char *c)
      {
          int i=0;
          while (c[i]!='')i++;
          return i;
      }
      int mystrcmp(char *c1,char *c2)
      {
          int i = 0, j = 0;
          for (; c1[i] != ''&&c2[i] != ''; i++)
          {
              if (c1[i] < c2[i])return -1;
              else if (c1[i] == c2[i])continue;
              else return 1;
          }
          return 0;
      }
      char* mystrcopy(char *c1, char *c2)
      {
          int i = 0, j = 0;
      
          while (c2[j] != '')c1[i++] = c2[j++];
      
          c1[i] = '';
          return c1;
      }
      char* mystrcat(char *c1, char *c2)
      {
      
          int i = 0, j = 0;
          while (c1[i] != '')i++;
      
          while(c2[j] != '')c1[i++] = c2[j++];
      
          c1[i] = '';
          return c1;
      }
      int main()
      {
          char c1[100] = "hello ", c2[100] = "woel";
          printf("%d
      ",mystrlen(c1));
          printf("%d
      ", mystrcmp(c1, c2));
          mystrcat(c1, c2);
          printf("%s
      ", c1);
          mystrcopy(c1, c2);
          printf("%s
      ", c1);
      
          system("pause");
          return 0;
      }

      2.输入一行字符串(单词和若干空格), 输出该行单词个数。

      Input____hello_________world_ how___are___you___

        Output:   5

    #include<stdio.h>
    #include<stdlib.h>
    
    int fun(char *c) 
    {
        int i = 0,cnt=0;
        while (c[i] != '')
        {
            if (c[i] == ' ')
            {
                i++;
                if (c[i] != ' ')cnt++;
            }
            else i++;
            
        }
        return cnt;
    }
    
    int main()
    {
        char c1[100] = "hello world how are you ";
        puts(c1);
        printf("%d",fun(c1));
    
    
        system("pause");
        return 0;
    }

    3.输入一行字符串(单词和若干空格),输出该行单词(每个单词一行)

    Input____hello_________world_ how___are___you___

    Output:   hello

                    world

                    how

                    are

                           You

    #include<stdio.h>
    #include<stdlib.h>
    
    void fun(char *c) 
    {
        int i = 0,cnt=0;
        while (c[i] != '')
        {
            if (c[i] == ' ')
            {
                i++;
                continue;
            }
            if (c[i] != ' ')
            {
                printf("%c", c[i]);
                i++;
                if (c[i] == ' ')printf("
    ");
            }
            
        }
    }
    
    int main()
    {
        char c1[100] = "hello world how are you ";
        puts(c1);
        fun(c1);
    
    
        system("pause");
        return 0;
    }

    4.输入一行字符串,把字符串翻转 

    Input: I____am__a__student

    Output: student__a__am____I

     

    1、直接翻转一下

    2、把每个单词找出来,原地自身翻转

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    void fun1(char *c)
    {
    int i = strlen(c) - 1;

    while (i >= 0)
    {
    printf("%c", c[i]);
    i--;
    }
    //printf(" ");
    }
    void strcut(char *s, char *sub) //用参数sub来保存结果,所以函数不用返回值了
    {
    int i=0, j = 0, k = 0;
    for (i = strlen(s)-1; i >=0 ; i--)
    {
    //printf("%c", s[i]);
    if (s[i] != ' ')
    {
    sub[j++] = s[i];
    if (s[i-1] == ' '||i==0)
    {
    sub[j] = '';
    fun1(sub); printf(" ");
    j = 0;
    }

    }

    }
    }


    int main()
    {
    char c1[] = "hello world how are you ";
    puts(c1);
    fun1(c1);
    printf(" ");
    char ps[100]; //用来保存截取子串的必须是数组,不能是指针
    static char s[] = "hello world how are you ";
    strcut(s, ps);
    //printf("%s ", ps);
    system("pause");
    return 0;
    }


  • 相关阅读:
    第二期冲刺会议3
    第二期站立会议2
    意见汇总及改进方案
    第二期站立会议1
    第一期站立会议7
    第一期站立会议6
    第一期站立会议5
    第一期站立会议4
    第一期站立会议3
    第一期站立会议2
  • 原文地址:https://www.cnblogs.com/cthon/p/8886034.html
Copyright © 2020-2023  润新知