c语言 9-5

1、

#include <stdio.h>

int str_char(char x[], int key)
{
    int len = 0;
    while(x[len])
        len++;
    int i;
    for(i = 0; i < len; i++)
    {
        if(x[i] == key)
            return i;
    }
    if(i == len)
        return -1;
}

int main(void)
{
    char str[128];
    printf("str: "); scanf("%s", str);
    puts("
=================");
    printf("position: %d
", str_char(str, 'c'));
    return 0;
}

2、

#include <stdio.h>

int str_char(char x[], int key)
{
    int len = 0;
    while(x[len])
        len++;
    int i = 0;
    while(1)
    {
        if(i == len)
            return -1;
        if(x[i] == key)
            return i;
        i++;
    }
}

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

3、

#include <stdio.h>

int str_char(char x[], int key)
{
    int len = 0;
    while(1)
    {
        if(x[len] == '
')
            break;
        len++;
    }
    x[len] = key;
    int i = 0;
    while(1)
    {
        if(x[i] == key)
            break;
        i++;
    }
    return i < len ? i : -1;
}

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

4、

#include <stdio.h>


int str_char(char x[], int key)
{
    int i = 0;
    while(1)
    {
        if(x[i] == key)
            return i;
        if(x[i] == '')
            return -1;
        i++;
    }
}

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

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