面试题第一弹

一、寻找字符串中最长的子字符串

思路分析:

规定字符串中遇到空格和逗号(,)为一个完整的子字符串,例如字符串"hello world,xiaoshijie"  这里有三个子字符串,分别为hello、world和xiaoshijie,最长的子字符串是xiaoshijie

(1)刚开始从字符串中找出两个子字符串,比较两个子字符串,并保存最长的子字符串和它的长度

(2)继续找到字符串中下一个子字符串,并且和步骤1中找到的最长的子字符串比较,继续保存最长的子字符串和它的长度

(3)循环步骤2,直到字符串尾

(4)返回最长的子字符串

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 char *searchString(char *source)
 6 {
 7     int len1=0,len2=0,i=0,k=0;
 8     char temp[100],temp2[100]; 
 9     char subString[100]; //最长的子字符串
10     char tempString[100];
11     memset(temp,0,sizeof(temp));
12     memset(temp2,0,sizeof(temp2));
13     memset(subString,0,sizeof(subString));
14     int flag = 0; //为0的时候是第一次比较两个子字符串,为1是第二次以后比较两个子字符串
15     int max = 0; //最长的子字符串
16     while(*source)
17     {
18         if(*source==' ' || *source == ',') //子字符串的结束符
19         {
20             if(flag == 0) 
21             {
22                 while(*source)
23                 {
24                     source++;
25                     if(*source==' ' || *source == ',')
26                     {
27                         memset(subString,0,sizeof(subString));
28                         max = (len1>len2)?len1:len2; //获取目前最长的子字符串的长度
29                         strcpy(subString,(len1>len2?temp:temp2)); //获取目前最长的子字符串
30                         i = 0;
31                         k = 0;
32                         len1 = 0;
33                         len2 = 0;
34                         memset(temp,0,sizeof(temp));
35                         memset(temp2,0,sizeof(temp2));
36                         flag = 1; 
37                         break;
38                     }
39                     temp2[k++] = *source;
40                     len2++;                    
41                 }
42             }
43             else
44                {
45                    memset(tempString,0,sizeof(tempString));
46                    strcpy(tempString,subString);
47                    memset(subString,0,sizeof(subString));
48                    strcpy(subString,(max>len1)?tempString:temp);
49                    max = (max>len1)?max:len1;
50                    len1 = 0;
51                    i = 0;
52                    memset(temp,0,sizeof(temp));
53                }
54           source++; //跳过' '和','
55           continue;
56         }
57         temp[i++] = *source;
58         len1++;
59         source++;
60     }
61     strcpy(subString,(max>len1)?subString:temp);
62     return subString;
63 }
64 
65 int main(int argc,char **argv)
66 {
67     char * temp = "hello world,xiaoshiije";
68     
69     char h[100] = {0};
70     strcpy(h,searchString(temp));
71     
72     printf("the longest substring:%s\n",h);
73     return 0;
74 }
View Code

 

 二、整形转换成二进制

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(void)
 5 {
 6     printf("N=");
 7     int n,i;
 8     scanf("%d",&n);
 9     int len=sizeof(int)*8;   //int型所占数据宽度
10     
11     for(i=0;i<len;i++)
12     {    
13         putchar('0'+((unsigned)(n<<i)>>(len-1)));    //先左移,再逻辑右移,就把二进制从高位到低位输出了
14 
15 // printf("%d",((unsigned)(n<<i)>>(len-1)));  //也可以这样输出
16 
17     }
18     //第二中方法
19     /*
20     for(i=0;i<len;i++)
21     {
22         putchar('0'+((unsigned)(n&(1<<(len-1)))>>len-1));  //将1左移,然后和n做按位与运算,最后逻辑右移,从高位到低位输出二进制
23         n<<=1;  //上边的一句总是对最高位进行处理,所以,这里就对n左移一位
24     }
25     printf("\n%d\n",n);
26     */
27 }
View Code

 

 三、计算一个字节里有多少位为1

思路分析:

(1)与1按位与(&),为真的话就是为1

(2)从左往右一位一位移动,重复步骤1

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int count(int val)
 6 {
 7     int cou = 0;
 8     while(val)
 9     {
10         cou += val&1; //计算其中的为1的位
11         val >>= 1; //右移一位
12     }
13     return cou;
14 }
15 int main(int argc,char *argv[])
16 {
17     int i = 3;
18     int temp = 0;
19     temp = count(i);
20     printf("%d\n",temp);
21     
22     return 0;
23 }
View Code

 

四、字符串转换成整数

思路分析:

(1)判断字符串的第一位是正数还是负数,用标志号sign保存起来,负数的话sign为-1,正数为1

(2)将数组从第二位开始一个一个往后移动,判断字符是否是在‘0’和‘9’之间的字符,如果是则转换成整数,不是则判断为非法字符

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int my_atoi(char *source)
 6 {
 7     int sign = 1; //sign为1为正,-1为负数
 8     int num = 0;
 9     while(*source)
10     {
11         if(*source == '-')
12         {
13             sign = -1;
14         }
15         else if(*source == '+')
16         {
17             sign = 1;
18         }
19         else if(*source >= '0' && *source <= '9')
20         {
21             num = (int)(num*10 + (*source - '0')); //转换成整形,-'0'是计算出字符和字符0ASCII码之间的差值
22         }
23      
24         else 
25         {
26             printf("the string invalid!!\n");
27             break;
28         }
29         source++;
30     }
31    return sign*num; 
32     
33 }
34 
35 int main(int argc,char *argv[])
36 {
37     char *p = "1230001111111";
38     int integer = 0;
39     integer = my_atoi(p);
40     printf("%d\n",integer);
41     return 0;
42 }
View Code

 

五、整数转换成字符串

思路分析:

(1)使用取余法(%),一位一位的分离出整数,把分离出来的数组保存在数组中

(2)分离的时候因为是从整数的个位开始的,所以需要倒序数组

代码:

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

char *itostr(int num)
{
    char temp[100] = {0};
    char *str = (char*)malloc(100*sizeof(char)); //记得分配空间
    //memset(temp,0,sizeof(temp));
    int i = 0,j = 0;
    while(num)
    {
        temp[i++] = num%10 + '0'; //+'0'算出与字符0之间的差值,得出字符
        num /= 10;
    }
    temp[i] = '\0'; 
    int tem = i;

    //倒序
    for(j=0;j<tem;j++)
    {
        str[j] = temp[--i];
    }
    str[j] = '\0';
    return str;  //返回的是一个指针
}

int main(int argc,char **argv)
{
    int num = 12312512;
    char *p = NULL;
    //sprintf(p,"%d",num); //第一种方法
    //printf("%s\n",p);
    printf("%s\n",itostr(num));
    return 0;
}
View Code

 

 六、字符串逆序

思路分析:

(1)先交换第一个和最后一个字符的位置

(2)接着交换第二个和倒数第二个字符的位置

(3)重复步骤2,直到交换到了中间的那个字符为止

代码:

 1 /*************************************************
 2 *将一个字符串反转
 3 **************************************************/
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include <string.h>
 7 
 8 void reverse(char *str)
 9 {
10     char c;
11     int length = strlen(str);
12     int i = 0;
13     for(i=0;i<(length/2);i++)
14     {
15         c = str[i];
16         str[i] = str[length-1-i];
17         str[length-1-i] = c;
18     }
19     str[length] = '\0';
20 }
21 
22 int main(int argc,char **argv)
23 {
24     char sou[10];
25     strcpy(sou,"world");
26     printf("before reverse:%s\n",sou);
27     reverse(sou);
28     printf("after reverse:%s\n",sou);
29     return 0;    
30 }
View Code

 

posted @ 2018-02-22 17:43  沙漠里的孤鲸  阅读(444)  评论(2编辑  收藏  举报