字符串t1t2t3t4

 

1.查找一个字符再另外一个字符串中第一次出现的下标
2.查找一个字符再另外一个字符串中第一次出现的地址
3.查找一个字符串再另外一个字符串中第一次出现的地址  abcccccde ccd
4.查找一个字符串中单词的个数(不考虑标点符号)"  a 23d fg9fg 89 sdf#include<stdio.h>

const char* t3(const char *s1,const char *s2);
int t4(const char *psz);

int main()
{
    int num = t4("     a 34fd dsf3d sdf 324 d    a");
    printf("%d\n",num);
    return 0;
}

int t4(const char *psz)
{
    int count = 0;
    const char *zou;
    while(*psz != '\0')
    {
        if(' ' == *psz)
        {
            psz++;
        }
        else
        {
            zou = psz;
            while(*zou != ' ' &&  *zou != '\0')
            {
                zou++;
            }
            while(psz < zou)
            {
                if(!(*psz >= 'a'  && *psz <='z' ))
                {
                    break;
                }
                psz++;
            }
            if(psz == zou)
            {
                count++;
            }
            else
            {
                while(*psz != ' ' && *psz != '\0')
                {
                    psz++;
                }
            }
        }
    }
    return count;
}

const char* t3(const char *s1,const char *s2)
{
    const char *jiS1 = NULL;
    const char *jiS2 = s2;
    while(*s1 != '\0')
    {
        if(*s1 == *s2)
        {
            //鏄涓€娆$浉绛夊悧锛?            if(NULL == jiS1)
            {
                jiS1 = s1;
            }
            s1++;
            s2++;
        }
        else
        {
            //璁板綍杩囧悧?
            if(NULL == jiS1)
            {
                s1++;
            }
            else
            {
                if('\0' == *s2)
                {
                    return jiS1;
                }
                s2 = jiS2;
                s1 = jiS1 + 1;
                jiS1 = NULL;
            }
        }
    }
    if('\0' == *s2)
    {
        return jiS1;
    }
    return NULL;
}

const char* t2(const char s1,const char *psz)
{
    while(*psz != '\0')
    {
        if(s1 == *psz)
        {
            return psz;
        }
        psz++;
    }
    return NULL;
}

int t1(const char s1,const char *psz)
{
    int i;
    for(i=0 ; psz[i]!='\0' ; i++)
    {
        if(s1 == psz[i])
        {
            return i;
        }
    }
    return -1;
}

你可能感兴趣的:(字符串t1t2t3t4)