算法实现c语言--03

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

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

    Input____hello_________world_ how___are___you___\n

      Output:   5

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

int fun(char *c) 
{
    int i = 0,cnt=0;
    while (c[i] != '\0')
    {
        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___\n

Output:   hello

                world

                how

                are

                       You

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

void fun(char *c) 
{
    int i = 0,cnt=0;
    while (c[i] != '\0')
    {
        if (c[i] == ' ')
        {
            i++;
            continue;
        }
        if (c[i] != ' ')
        {
            printf("%c", c[i]);
            i++;
            if (c[i] == ' ')printf("\n");
        }
        
    }
}

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("\n");
}
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] = '\0';
fun1(sub); printf(" ");
j = 0;
}

}

}
}


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

 


posted @ 2018-04-19 20:24  CTHON  阅读(402)  评论(0编辑  收藏  举报