笔试题 落日映苍穹つ 2022-07-16 00:51 169阅读 0赞 strstr #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> const char *my_strstr(const char *str,const char *sub_str) { int i; for(i=0;str[i]!='\0';i++) { int tmp = i; int j=0; while(str[i++]==sub_str[j++]) { if(sub_str[j]=='\0') return &str[tmp]; } i = tmp; } return NULL; } int main(void) { char *s1="12345"; char *s2="34"; printf("%s\n",my_strstr(s1,s2)); } strcpy #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> char *my_strcpy(char *dest, const char *src) { assert(dest); assert(src); char *tmp = dest; while((*dest++=*src++)!='\0') ; return tmp; } int main(int argc, char ** argv) { char a[10]; char *b = "hello"; printf("%s\n",my_strcpy(a,b)); return 0; } strncpy #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> char *my_strncpy(char *dst, const char *src,size_t count) { assert(dst&&src); char *tmp = dst; while(count) { if((*dst=*src)!=0) { dst++; src++; } count--; } *dst = '\0'; return tmp; } int main() { char a[5] = "hello"; char *b = "12345"; printf("%s\n",my_strncpy(a,b,6)); } strcat #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> char *my_strcat(char *dest, const char *src) { assert(dest); assert(src); char *tmp = dest; while(*dest) dest++; while((*dest++=*src++)!='\0') ; return tmp; } int main(int argc, char ** argv) { char a[10] = "world"; char *b = "hello"; printf("%s\n",my_strcat(a,b)); return 0; } strncat #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> char *my_strncat(char *dest, const char *src, size_t count) { assert(dest); assert(src); char *tmp = dest; if(count) { while(*dest) dest++; while((*dest++=*src++)!='\0') <span> </span>{ if(--count==0) { *dest = '\0'; break; } <span> </span>} } return tmp; } int main(int argc, char ** argv) { char a[10] = "world"; char *b = "hello"; printf("%s\n",my_strncat(a,b,3)); return 0; } strcmp #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> int my_strcmp(const char *cs, const char *ct) { assert(cs); assert(ct); unsigned char c1,c2; while(1) { c1 = *cs++; c2 = *ct++; if(c1!=c2) return c1 < c2 ? -1 : 1; if(!c1) break; } return 0; } int main(int argc, char ** argv) { char a[10] = "gello"; char *b = "hello"; printf("%d\n",my_strcmp(a,b)); return 0; } 字符串逆序 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> char *reverse(char *dest, const char *src) { assert(dest); assert(src); char *tmp = dest; int len,i; len = strlen(src); dest[len] = '\0'; for(i=0;i<len;i++) { *(dest+i) = *(src+len-i-1); } return tmp; } int main(int argc, char ** argv) { char a[10]; char *b = "abcde"; printf("%s\n",reverse(a,b)); return 0; } 冒泡排序 #include <stdio.h> #include <stdlib.h> #include <string.h> static void bubble_sort(int array[],int n) { int i,j,tmp; for(j=0;j<n-1;j++) { for(i=0;i<n-1-j;i++) { if(array[i]>array[i+1]) { tmp = array[i]; array[i] = array[i+1]; array[i+1] = tmp; } } } } int main (int argc, char **argv) { int a[6] = {5,2,6,4,1,3}; bubble_sort(a,6); int i = 0; for(i;i<6;i++) printf("%d\n",a[i]); return 0; } memcpy void *memcpy(void *dest, const void *src, size_t count) { char *tmp = dest; const char *s = src; while (count--) *tmp++ = *s++; return dest; } static关键字 (1)函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值; (2)在模块内的static全局变量可以被模块内所用函数访问,但不能被模块外其它函数访问; (3)在模块内的static函数只可被这一模块内的其它函数调用,这个函数的使用范围被限制在声明它的模块内; const关键字 (1)欲阻止一个变量被改变,可以使用const关键字。在定义该const变量时,通常需要对它进行初始化,因为以后就没有机会再去改变它了; (2)对指针来说,可以指定指针本身为const,也可以指定指针所指的数据为const,或二者同时指定为const; (3)在一个函数声明中,const可以修饰形参,表明它是一个输入参数,在函数内部不能改变其值; (4)对于类的成员函数,若指定其为const类型,则表明其是一个常函数,不能修改类的成员变量; volatile关键字 定义为volatile的变量表示该变量可能被意想不到的改变,使用该变量时,必须每次重新从内存中读取数据,而不是使用保存在寄存器中的备份。 例子: (1)并行设备的硬件寄存器;如状态寄存器 (2)一个中断服务子程序中会访问的非自动变量; (3)多线程应用中被几个任务共享的变量;
相关 mysql查询笔试题_mysql笔试题 \-- 查询每位会员在7月份的订单数、订单总额,并以订单总额进行降序排列 select dimMemberID, -- 会员ID count(salesID) as ‘o 旧城等待,/ 2022年10月27日 05:21/ 0 赞/ 197 阅读
相关 笔试题② 1、写出几种常见的设计模式,并详细列举出一种。 单例设计模式,工厂设计模式,适配器设计模式,模板设计模式,装饰设计模式 2、描述下tcp接收数据的过程。 1:建立服务器端 ﹏ヽ暗。殇╰゛Y/ 2022年10月03日 01:00/ 0 赞/ 157 阅读
相关 笔试题 输入一个数组大小n,步长r和数组a, 做一个操作,使得b\[i\]=MED(a\[i\], a\[i+1\]modn, ... a\[i+r-1\]modn),MED方法是返 待我称王封你为后i/ 2022年07月16日 10:44/ 0 赞/ 210 阅读
相关 笔试题 strstr include <stdio.h> include <stdlib.h> include <string.h> include 落日映苍穹つ/ 2022年07月16日 00:51/ 0 赞/ 170 阅读
相关 面试笔试题 1.一维数组默认初始化问题 如果不对数组进行任何初始化操作, 仅定义一个数组, 那么数组中这些元素的值是不确定的, 是系统中随机的一个值。 50.const 偏执的太偏执、/ 2022年07月13日 14:30/ 0 赞/ 212 阅读
相关 面试笔试题 1.写一个函数,返回一个字符串中只出现一次的第一个字符 [第一个只出现一次的字符(包含代码-C++)][-C] (1)用查找子串的方式,每次遍历一 逃离我推掉我的手/ 2022年07月13日 13:54/ 0 赞/ 228 阅读
相关 笔试题 最近做了一些公司的笔试题,将所记部分整理一下。 1、查询该目录及子目录下所有的以.conf为后缀的文件:find . -name “\.conf”由此题扩展到linux的指 左手的ㄟ右手/ 2022年06月08日 08:28/ 0 赞/ 212 阅读
相关 笔试题 搜狗2017年校招笔试编程题题目: 在一个圆形上的若干点,点的位置以角度的形式表示,(0-360),输入若干的点的位置,点的距离以劣弧对应角度表示,求距离最远的两个点的角 小咪咪/ 2022年06月08日 02:44/ 0 赞/ 275 阅读
相关 华为笔试题 今天,帮同学看了到华为的笔试题,题目如下: 举办一场8小时的聚会,时间段从12:00到20:00点,让来访的客人事先填好到达的时间和离开的时间,为了掌握聚会期间的座位数目,需 迈不过友情╰/ 2022年05月16日 04:41/ 0 赞/ 257 阅读
还没有评论,来说两句吧...