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("\n=================");
    printf("position: %d\n", 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\n", 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] == '\n')
            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\n", 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] == '\0')
            return -1;
        i++;
    }
}

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

 

posted @ 2021-05-27 11:25  小鲨鱼2018  阅读(71)  评论(0编辑  收藏  举报