您当前的位置: 首页 >  嵌入式

MTK联发科2021 嵌入式C笔试题分析

发布时间:2020-07-19 09:47:28 ,浏览量:0

题目-分割字符串

因为题目是考试的同学凭记忆记下来的,没有记得特别仔细,题目只是写了个大概,我也是凭自己的想法来理解。

输入

aaa;bbb;ccc

输出

aaa
bbb
ccc
/*--------------------------------------------*/
输入

,hello,hello,,

输出

hello
hello

这个题目会使用到什么知识点呢?

C库字符串分割函数 strtok

函数声明:

char *strtok(char *str, const char *delim)

作用:  使用分隔符delim分解字符串str。

返回值

该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。

注意:分割处理后原字符串 str 会变。

举个例子
#include 
#include 
 
int main () {
   char str[80] = "my,name,is,weizenan,happy,,,,";
   const char s[2] = ",";
   char *token;
   
   /* 获取第一个子字符串 */
   token = strtok(str, s);
 
   /* 继续获取其他的子字符串 */
   while( token != NULL ) {
      printf( "%s\n", token );
      printf("str:%s\n",str);
      token = strtok(NULL, s);
   }
   printf("\n%s\n",str);
   return(0);
}

输出

my
str:my
name
str:my
is
str:my
weizenan
str:my
happy
str:my

my

--------------------------------
Process exited after 0.02338 seconds with return value 0
请按任意键继续. . .
上面题目的答案
# include 
# include 

int split(char *src,const char *separator,char **dest) 
{
 /*
  src 需要分割的字符串 
  separator 指定的分割字符
  dest 接收子字符串的数组
  返回分割字符串个数 
 */
     char *pNext = NULL;
     int count = 0;
     /*如果传入的地址为空或长度为0,直接终止 */
     if (src == NULL || strlen(src) == 0) 
        return (0);
     /*如未指定分割的字符串,直接终止 */
     if (separator == NULL || strlen(separator) == 0) 
        return (0);
     /*第一次,先保存下第一次的分割结果*/
     pNext = (char *)strtok(src,separator); 
     while(pNext != NULL) {
        /*把分割结果存放在 字符串数组里面*/ 
          *dest++ = pNext;
          ++count;
         /*必须使用(char *)进行强制类型转换*/
         pNext = (char *)strtok(NULL,separator);  
    }  
    return count;
}  

int main()
{
 int i;
 char buf[30];/*"aaa;bbb;ccc"*/
 /*存放分割结果*/ 
 char *revbuf[8] = {0};
 
 /*分割后子字符串的个数*/
 int num = 0;
 
 gets(buf);
 num = split(buf,";",revbuf); //调用函数进行分割 
 
 /*输出分割结果*/
 for(i = 0;i < num; i ++) 
 {
  printf("%s\n",revbuf[i]);
 }

 return 0;
}

输出

124;oajsidfji;jijdiji990;;9090;90
124
oajsidfji
jijdiji990
9090
90

--------------------------------
Process exited after 15.47 seconds with return value 0
请按任意键继续. . .
其他题目

这部分的答案先不放出来,我觉得大家可以讨论看看,有哪道题不明白的,可以在留言回复,也可以自己去寻找答案。

这题应该是送分题

int Add(int Number-1,int Number2)
{
  int Sum = Number-1;
  Sum+=Number2;
  retrun sum;
}

which one is correct (multi selections) 1)There will be compile errors at the first line 2)Number-1 is an illegal varibale name 3)Number2 is an illegal variable name 4)Sum and sum refer to the same variable

这题应该也是送分题

int a = 0x0077;
int b = 0x0088;
int c = 0xff00;
int d = a | b | c;
int e = c & d;
d=?
e=?
int sum(int n)//calc the sum for 1+2+3+...n
{
 int i=1;
 intnSum =0;
 while(i<=n)
 nSum+= i;
 i++;

 retrun nSum;
}

which ones are correct

1)Use while loop is wrong use if loop isthe only choice. 2)we should change all int to unsigned int in the codes 3)at line 007, we should change (i<=n)to (i

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    106593博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.3968s