常见的字符串面试问题(二)

13 篇文章 0 订阅

1)写出在母串中查找子串出现次数的代码.

int count1(char* str,char* s)

{

    char* s1;

    char* s2;

    int count = 0;

    while(*str!='\0')

    {

        s1 = str;

        s2 = s;

        while(*s2 == *s1&&(*s2!='\0')&&(*s1!='\0'))

        {

            s2++;

            s1++;

        }

        if(*s2 == '\0')

            count++;

        str++;

    }

    return count;

}

2)查找第一个匹配子串位置,如果返回的是s1长度len1表示没有找到

size_t find(char* s1,char* s2)

    {

        size_t i=0;

         size_t len1 = strlen(s1)

        size_t len2 = strlen(s2);

        if(len1-len2<0) return len1;

        for(;i<len1-len2;i++)

        {

            size_t m = i;

            for(size_t j=0;j<len2;j++)

            {

                if(s1[m]!=s2[j])

                    break;

                m++;

            }

            if(j==len)

                break;

        }

        return i<len1-len2?i:len1;

    }

3)实现strcpy函数

char *strcpy(char *destination, const char *source)

{

    assert(destination!=NULL&&source!=NULL);

    char* target = destinaton;

    while(*destinaton++=*source++);

    return target ;

}

出现次数相当频繁

4)实现strcmp函数

int strcmp11(char* l,char* r)

{

    assert(l!=0&&r!=0);

    while(*l == *r &&*l != '\0') l++,r++;

    if(*l > *r)

        return 1;

    else if(*l == *r)

        return 0;

    return -1;

}

5) 实现字符串翻转

void reserve(char* str)

{

    assert(str != NULL);

    char * p1 = str;

    char * p2 = str-1;

    while(*++p2);         //一般要求不能使用strlen

    p2 -= 1;

    while(p1<p2)

    {

        char c = *p1;

        *p1++ = *p2;

        *p2-- = c;

   }

}

6)、用指针的方法,将字符串“ABCD1234efgh”前后对调显示

//不要用strlen求字符串长度,这样就没分了

代码如下:

    char str123[] = "ABCD1234efgh";

    char * p1 = str123;

    char * p2 = str123-1;

    while(*++p2);

    p2 -= 1;

    while(p1<p2)

    {

        char c = *p1;

        *p1++ = *p2;

        *p2-- = c;

    }

7) 给定字符串A和B,输出A和B中的最大公共子串。比如A="aocdfe" B="pmcdfa" 则输出"cdf"

#i nclude<stdio.h>

#i nclude<stdlib.h>

#i nclude<string.h>

char *commanstring(char shortstring[], char longstring[])

{

    int i, j;

    char *substring=malloc(256);

    if(strstr(longstring, shortstring)!=NULL)              //如果……,那么返回shortstring

        return shortstring;

    for(i=strlen(shortstring)-1;i>0; i--)                 //否则,开始循环计算

    {

        for(j=0; j<=strlen(shortstring)-i; j++)

        {

            memcpy(substring, &shortstring[j], i);

            substring[i]='\0';

            if(strstr(longstring, substring)!=NULL)

            return substring;

        }

    }

    return NULL;

}

main()

{

    char *str1=malloc(256);

    char *str2=malloc(256);

    char *comman=NULL;

    gets(str1);

    gets(str2);

    if(strlen(str1)>strlen(str2))                         //将短的字符串放前面

        comman=commanstring(str2, str1);

    else

        comman=commanstring(str1, str2);

    printf("the longest comman string is: %s\n", comman);

}

8) 判断一个字符串是不是回文

int IsReverseStr(char *str)

{

    int i,j;

    int found=1;

    if(str==NULL)

        return -1;

    char* p = str-1;

    while(*++p!= '\0');

    --p;

    while(*str==*p&&str<p) str++,p--;

    if(str < p)

        found = 0;

    return found;

}

9)写函数完成内存的拷贝

void* memcpy( void *dst, const void *src, unsigned int len )

{

    register char *d;

    register char *s;

    if (len == 0)

        return dst;

    if ( dst > src )   //考虑覆盖情况

    {

        d = (char *)dst + len - 1;

        s = (char *)src + len - 1;

        while ( len >= 4 )   //循环展开,提高执行效率

        {

            *d-- = *s--;

            *d-- = *s--;

            *d-- = *s--;

            *d-- = *s--;

            len -= 4;

        }

        while ( len-- )

        {

            *d-- = *s--;

        }

    }

    else if ( dst < src )

    {

        d = (char *)dst;

        s = (char *)src;

        while ( len >= 4 )

        {

            *d++ = *s++;

            *d++ = *s++;

            *d++ = *s++;

            *d++ = *s++;

            len -= 4;

        }

        while ( len-- )

        {

            *d++ = *s++;

        }

    }

    return dst;

}

10)写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)

功能:

在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回

9,outputstr所指的值为123456789

int continumax(char *outputstr, char *inputstr)

{

    char *in = inputstr, *out = outputstr, *temp, *final;

    int count = 0, maxlen = 0;

    while( *in != '\0' )

    {

        if( *in > 47 && *in < 58 )

        {

            for(temp = in; *in > 47 && *in < 58 ; in++ )

            count++;

        }

    else

    in++;

    if( maxlen < count )

    {

        maxlen = count;

        count = 0;

        final = temp;

    }

    }

    for(int i = 0; i < maxlen; i++)

    {

        *out = *final;

        out++;

        final++;

    }

    *out = '\0';

    return maxlen;

}

11) 编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。

char * search(char *cpSource, char ch)

{

         char *cpTemp=NULL, *cpDest=NULL;

         int iTemp, iCount=0;

         while(*cpSource)

         {

                 if(*cpSource == ch)

                 {

                          iTemp = 0;

                          cpTemp = cpSource;

                          while(*cpSource == ch)

                                   ++iTemp, ++cpSource;

                          if(iTemp > iCount)

                                iCount = iTemp, cpDest = cpTemp;

                      if(!*cpSource)

                            break;

                 }

                 ++cpSource;

     }

     return cpDest;

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值