字符串

来源:互联网 发布:天小猫淘宝信誉查询 编辑:程序博客网 时间:2024/06/03 19:12

1.wertyu 常量数组

把手放在键盘上时,稍不注意就会往右错一位。这样,输入Q会变成输入W,输入J会变成输入K等。

输入一个错位后敲出的字符串(所有字母均大写),输出打字员本来想打出的句子。输入保证合法,即一定是错位之后的字符串。例如输入中不会出现大写字母A。
样例输入:
O S, GOMR YPFSU/
样例输出:

I AM FINE TODAY.


采用常量数组

#include <stdio.h>char *s = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";int main(){int i,c;while ((c = getchar()) != EOF){for (i=1;s[i] && s[i]!=c;i++);if(s[i]) putchar(s[i-1]);else putchar(c);}return 0;}


2.TeX括号 标志变量

在TeX中,做双引号是“,右双引号是”。输入一篇包含双引号的文章,输出转换成TeX格式。

输入:"To be or not to be,"quoth the Bard,"that is the question".

输出:“To be or not to be,”quoth the Bard,“that is the question”.

#include <stdio.h>/* *判断左右引号,q标志变量 * */int main(){int c,q=1;while((c=getchar())!=EOF){if(c=='"'){printf("%s",1?"``":"''");q=!q;}else printf("%c",c);}return 0;}

3.周期串 枚举各周期+标志变量

如果一个字符串可以由某个长度为K的字符串重复多次得到,我们说该串以K为周期,例如: 
abcabcabcabc以3为周期(注意,它也以6和12为周期)。输入一个长度不超过80的串,输出它的最小周期

举例:输入:hoahoahoa 
输出: 3

#include <stdio.h>#include <string.h>int main(){char word[100];scanf("%s",word);int len = strlen(word);for(int i = 1;i<=len;i++) if (len % i == 0){int ok = 1;for(int j=i;j<len;j++)if(word[j]!=word[j%i]){ ok = 0; break;}if(ok){printf("%d\n",i);break;}}return 0;}