c语言 11-6

1、

#include <stdio.h>

char *str_chr(char *x, int key)
{
    while(*x)
    {
        if(*x == key)
        {
            char *t = x;
            return t;
        }
        x++;
    }
    return NULL;
}

int main(void)
{
    char str[128];
    printf("str: "); scanf("%s", str);
    
    printf("result: %s
", str_chr(str, 'c'));
    
    return 0;
}

2、

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

char *str_c(char *x, int key)
{
    int i, len = strlen(x);
    
    for(i = 0; i < len; i++)
    {
        if(*x++ == key)
        {
            char *t = x - 1;
            return t;
        }
    }
    return NULL;
}

int main(void)
{
    char str[128];
    printf("str:  "); scanf("%s", str);
    
    printf("result: %s
", str_c(str, 'd'));
    return 0;
}

3、

#include <stdio.h>

char *str_cho(char *x, int key)
{
    char *t;
    int j = 0;
    while(*x)
    {
        if(*x == key)
        {
            t = x;
            j++;
            break;
        }
        x++;
    }
    if(j == 0)
        t = NULL;
    return t;
}

int main(void)
{
    char str[128];
    printf("str:  "); scanf("%s", str);
    
    printf("result:  %s
", str_cho(str, 'e'));
    return 0;
}

4、

#include <stdio.h>

char *str_ch(char *x, int key)
{
    char *t;
    while(*x)
    {
        if(*x == key)
        {
            t = x;
            break;
        }
        else
            t = NULL;
        x++;
    }
    return t;
}

int main(void)
{
    char str[128];
    printf("str: "); scanf("%s", str);
    
    printf("result:  %s
", str_ch(str, 'f'));
    
    return 0;
}

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14833044.html