继续字符串练习
来源:互联网 发布:方正字体打包下载 mac 编辑:程序博客网 时间:2024/06/07 01:38
1.编写一个C函数,将”I am from shanghai ”倒置为”shanghai from am I”,及将句子中的单词位置倒置,而不改变单词内部结构.
#include <stdio.h> #include <string.h> void reverse(char a[], int len) { int i, j; char tmp = 0; int n = len - 1; for(i = 0; i < n; i++) { len--; for(j = 0; j < len; j++) { tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } } void re_reverse(char *a) { int n = 0; while(1) { n++; if((*a) == ' ' || (*a) == '\0') { reverse(a - n + 1, n - 1); n = 0; } if((*a) == '\0') { break; } a++; } } int main() { char a[] = "I am from shanghai"; int len; len = strlen(a); reverse(a, len); re_reverse(a); printf("the string after change is:%s\n", a); return 0; }
2.输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
#include <stdio.h> #include <string.h> int flag_h = 0; int flag_t = 0; char * head(char *s, char *h) { int num_h = 0; int len_h = 0; char *tmp = h; len_h = strlen(h); if(s == NULL || h == NULL) { return NULL; } if(strlen(s) < strlen(h)) { return NULL; } while(*s != '\0' && flag_h == 0) { while(*s == *tmp) { num_h++; s++; tmp++; if(num_h == len_h) { flag_h = 1; tmp = s - num_h; break; } } if(num_h != 0 && num_h != len_h) { num_h = 0; tmp = h; s--; } s++; } if(flag_h == 1) { return tmp; } else { printf("illegal string!\n"); } } char * tail(char *s, char *t) { int num_t = 0; int len_t; char *tmp = t; len_t = strlen(t); if(s == NULL || t == NULL) { return NULL; } if(strlen(s) < strlen(t)) { return NULL; } if(flag_h == 1) { while(*s != '\0' && flag_t == 0) { while(*s == *tmp) { s++; tmp++; num_t++; if(num_t == len_t) { flag_t = 1; tmp = s - 1; break; } } if(num_t != 0 && num_t != len_t) { num_t = 0; tmp = t; s--; } s++; } } if(flag_t == 1) { return tmp; } else { printf("illegal string!\n"); return NULL; } } void display(char *start, char *end) { int n; int i; n = end - start + 1; if(n != 0) { for(i = 0; i < n; i++) { printf("%c", *start); start++; } printf("\n"); } else { printf("no data!\n"); } } int main() { char *start = NULL; char *end = NULL; char s[100]; char h[] = "head"; char t[] = "tail"; printf("enter a string(<100):\n"); scanf("%s", s); start = head(s, h); end = tail(s, t); display(start, end); return 0; }
0 0
- 继续字符串练习
- 继续华为上机题练习
- 字符串练习
- 字符串 练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 字符串练习
- 018day(继续学习字符串)
- 继续第一章,第二章的练习
- Linux 网卡丢包严重
- u盘安装windows2008r2 错误
- Android学习日志——第4天
- Redis之——Redis 的安装与使用(单节点)
- c++实验6-矩阵求和
- 继续字符串练习
- ubuntu下编写简单的helloworld程序
- 关于博弈论的一些解释
- 微服务分布式事务落地方案
- js中的splice用法
- H-找出n的倍数只能由0和1构成
- c/c++整理--实现删除字符串所有指定字符
- Git命令总结
- 向着实用主义至上的世界呐喊——《三傻大闹宝莱坞》观后感