前言:
现时你们对“c语言函数strstr”大致比较着重,同学们都想要分析一些“c语言函数strstr”的相关文章。那么小编在网络上网罗了一些关于“c语言函数strstr””的相关资讯,希望大家能喜欢,兄弟们一起来学习一下吧!1.strcat()、strncat()字符串追加函数
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>int main0201()//字符串追加函数,注意字符串1有足够空间容纳字符串2{ char a[100] = "abc"; char b[100] = "defg"; strcat(a, b);//将字符串b追加到a之后,并输出a printf("%s\n", a); return 0;}int main0202(){ char a[100] = "abc"; char b[100] = "defg"; strncat(a, b,1);//将字符串b追加到a之后,并输出a printf("%s\n", a); return 0;}
2.strcmp()、strncmp()字符串比较函数
#include<stdio.h>int main0301()//字符串比较{ char a[100] = "abc"; char b[100] = "abcd"; if (strcmp(a, b)==0)//比较a,b是否相同 printf("相同"); else printf("不同"); return 0;}int main0302()//字符串有限比较{ char a[100] = "abc"; char b[100] = "abcd"; if (strncmp(a, b,4) == 0)//比较a,b的前n个字符是否相同 printf("相同"); else printf("不同"); return 0;}
3.strcpy()字符串拷贝函数
#include<stdio.h>int main0401()//字符串拷贝,将b拷贝到a中并覆盖a内容{ char a[100] = "abc"; char b[100] = "12345"; strcpy(a, b); printf("%s\n", a); return 0;}int main0402(){ char a[100] = "abc"; char b[100] = "12345"; strncpy(a, b,sizeof(a)-1);//将a字符串放满 printf("%s\n", a); return 0;}
4.sprintf()格式化输入到字符串函数
#include<stdio.h>int main0501(){ char a[100]; sprintf(a, "%s", "hello world"); printf("%s\n", a); return 0;}
5.sscanf()字符串读取函数
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>int main0601(){ char a[100] = "10+10"; int b; int c; sscanf(a, "%d+%d", &b, &c); printf("%d+%d=%d\n", b, c, b + c);}int main0602(){ char a[100] = "43*25="; int i, j; char c; sscanf(a, "%d%c%d=", &i, &c, &j); int res=0; switch (c) { case'+': res = i + j; break; case'-': res = i - j; break; case'*': res = i * j; break; case'/': res = i / j; break; default: res = 0; } printf("%d%c%d=%d", i, c, j, res);}
6.strchr()字符查找函数
#include<stdio.h>#include<stdlib.h>int main0701(){ char a[100] = "hello world"; char* s; s = strchr(a, 'l'); if(s!=NULL) printf("%s\n", s); return 0;}
7.strstr()字符串中查找子串
#include<stdio.h>#include<stdlib.h>int main0801()//字符串中查找子串{ char a[100] = "hello world"; char *s; s = strstr(a, "llo"); printf("%s\n", s); return 0;}
8.strtok()字符串分割函数
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>int main0901(){ char a[100] = "abc_bce_123_456"; char* s; s = strtok(a, "_"); printf("%s\n", s); s = strtok(NULL, "_");//第二次调用时,参数写NULL printf("%s\n", s); s = strtok(NULL, "_"); printf("%s\n", s); return 0;}int main0902(){ char a[100] = "abc_bce_123_456"; char* s; s = strtok(a, "_"); while (s) { printf("%s\n", s); s = strtok(NULL, "_"); } return 0;}
9.atoi函数
#include<stdio.h>#include<stdlib.h>int main10_01()//atoi(){ char a[] = "123";//将字符串转化为整数 atoi() char b[] = "456"; int i = atoi(a) + atoi(b); printf("%d\n", i); return 0;}
10.atof函数
#include<stdio.h>#include<stdlib.h> int main10_02()//atof{ char a[100] = "3.555"; double f = atof(a); printf("%f\n", f); return 0;}
11.将一个数字追加到字符串之后
#include<stdio.h>#include<stdlib.h>int main10_03()//将100追加到abc之后{ char a[100] = "222"; char str1[100] = "abc"; int num = 100; sprintf(a, "%d", num); strcat(str1, a); printf("%s\n", str1); return 0;}
12.将字符串中的值求出
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>int main011(){ char a[100] = "15+20=;22-11=;13*50=;70/20="; char b[100] = {0}; char* p = strtok(a, ";"); while (p) { int i, j; char c; int sum = 0; sscanf(p, "%d%c%d=", &i, &c, &j); switch (c) { case '+': sum = i + j; break; case '-': sum = i - j; break; case '*': sum = i * j; break; case '/': sum = i / j; break; default: sum = NULL; break; } char temp[10] = {0}; sprintf(temp, "%s%d;", p ,sum); strcat(b, temp); p = strtok(NULL, ";"); } strcpy(a, b); printf("%s\n", a); return 0;}
13.z自定义函数的调用和使用
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>int add(int a, int b)//自定义函数 { return a + b;}void test()//定义一个自定义函数,没有返回值也没有参数 { printf("test\n");} void test1();//声明需调用的函数void test2(int a);int mmax(int a, int b){ return (a > b) ? a : b;}int main012(){ int i = 2; int j = 3; int c = add(i, j);//调用函数 printf("%d\n", c); test();//调用一个没参数,没返回值的函数 test1(); test2(i);//i是实参,实参可以是变量、常量也可以是表达式 int k=mmax(i, j); printf("%d\n", k); return 0;}void test1(){ printf("test1\n");}void test2(int a)//其中a为形参{ printf("a=%d\n", a);}
14.递归函数
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>void test3(int n){ if (n < 10) { test3(n + 1); } printf("n=%d\n", n);}int main14_01(){ int a = 0; test3(a); return 0;}
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>int age(int n)//n个人,后面的人比前面的人大2岁,求第n个人多大,第一个10岁{ if (n == 1)//递归结束条件 return 10; return age(n - 1) + 2;}int main14_02(){ int a = 5; printf("%d\n", age(a)); return 0;}
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>void to_bin(unsigned int n)//将一个十进制数转换为二进制{ int i = n % 2; if (n >= 2) { to_bin(n / 2); } printf("%d\n", i);}int main14_03(){ int a = 13; scanf("%d", &a); to_bin(a); return 0;}
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>char hex_char(unsigned int n)//将十进制数转换为16进制{ switch (n) { case 1: return '1'; case 2: return '2'; case 3: return '3'; case 4: return '4'; case 5: return '5'; case 6: return '6'; case 7: return '7'; case 8: return '8'; case 9: return '9'; case 10: return 'a'; case 11: return 'b'; case 12: return 'c'; case 13: return 'd'; case 14: return 'e'; case 15: return 'f'; } return '0';}void to_hex(unsigned int n){ int i = n % 16; if (n >= 16) to_hex(n / 16); printf("%c", hex_char(i));}int main14_04(){ int a; scanf("%d", &a); to_hex(a); printf("\n"); return 0;}
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>int fib(int n)////斐波那契数列 0,1,1,2,3,5,8,13,21,34,55{ if (n == 1) return 1; if (n == 2) return 2; return fib(n - 1) + fib(n - 2);}int main(){ int i; for (i = 1; i < 20; i++) { printf("%d\n", fib(i)); } return 0;}
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #c语言函数strstr