- UID
- 65892
注册时间2010-3-1
阅读权限50
最后登录1970-1-1
感悟天道
TA的每日心情 | 慵懒 2024-12-4 10:07 |
---|
签到天数: 444 天 [LV.9]以坛为家II
|
本帖最后由 sdnyzjzx 于 2011-1-17 09:18 编辑
现在有点越学越模糊的感觉了,记录一下字符串操作的代码,敬请批评指正。- #define total 30
- char c;
- _csh(char *p,char *a)
- {
- int i=0;
- for(i=0;i < total;i++)
- {
- *(p+i)=0;
- }
- for(i=0;i<10;i++)
- {
- *(a+i)=0;
- }
- }
- _len(char *p)
- {
- int i=0;
- while(*(p+i))
- {
- i++;
- }
- return(i);
- }
- _show(char *p,int n)
- {
- int i=0;
- for(i=0;i<n;i++)
- {
- printf("%c",*(p+i));
- }
- printf("\n");
- }
- _find1(char *p)
- {
- int i=0,j=0;
- printf("\n");
- while(*(p+i))
- {
- if(c==*(p+i))
- {
- printf("No.%d ",i+1);
- j++;
- }
- i++;
- }
- if(j==0)
- {
- printf("There are not any char what you find !\n");
- }
- else
- {
- printf("is char '%c'.\n",c);
- printf("There are %d '%c' in the string.\n\n",j,c);
- }
- }
- _find2(char *p,char *a,int s)
- {
- int k=0,m=0,j=0,i=0;
- m=_len(a);
- for(i=0;i<s-m;i++)
- {
- if(*a == *(p+i))
- {
- k++;
- while(*(a+k) == *(p+i+k))
- {
- if(k == m-1)
- {
- j++;
- printf("No.%d ",i+1);
- break;
- }
- k++;
- }
- }
- else
- {
- k=0;
- }
- }
- if(j==0)
- {
- printf("There are not any string what you find !\n");
- }
- else
- {
- printf("is the string what you find : '");
- for(i=0;i<m;i++)
- {
- printf("%c",*(a+i));
- }
- printf("'\n");
- printf("There are %d in the string.\n",j);
- }
- }
- _Stob(char *p)
- {
- int i=0;
- while(*(p+i))
- {
- if( *(p+i)>='a' && *(p+i)<='z' )
- {
- *(p+i) &= 0x0df;
- }
- i++;
- }
- }
-
- _Btos(char *p)
- {
- int i=0;
- while(*(p+i))
- {
- if( *(p+i)>='A' && *(p+i)<='Z' )
- {
- *(p+i) |= 0x20;
- }
- i++;
- }
- }
-
- main()
- {
- char *p = (char *)malloc( 30 * sizeof(char));
- char a[10];
- int s1=0,s2=0,s=0;
- _csh(p,a);
- clrscr();
- /* 输入字符串1 */
- printf("Please input string1 : ");
- scanf("%30s",p);
- s1 = _len(p);
- printf("String1 is : ");
- _show(p,s1);
- printf("String1 lenth is : %d\n",s1);
- printf("\n");
- /* 输入字符串1 */
- printf("Please input string2 : ");
- scanf("%30s",p+s1);
- s2=_len(p);
- printf("String2 is : ");
- _show(p+s1,s2-s1);
- printf("String2 lenth is : %d\n",s2-s1);
- printf("\n");
- /* 两个字符串长度和不大于总长度打印输出其和,否则输出第一个字符串 */
- if(s2>total)
- {
- printf("Your input is too long !\n");
- s=s1;
- }
- else
- {
- s=s2;
- }
- printf("String result is : ");
- _show(p,s);
- printf("String result lenth is : %d\n",s);
- printf("\n");
- /* 查找一个字符 */
- printf("Please input a char what you want to find : ");
- c=getch();
- printf("%c",c);
- _find1(p);
- /* 查找一个字符串 */
- printf("Please input a string what you want to find : ");
- scanf("%10s",a);;
- _find2(p,a,s);
-
- /* 小写变大写 */
- printf("\n");
- printf("Small to Big is : ");
- _Stob(p);
- _show(p,s);
- /* 大写变小写 */
- printf("\n");
- printf("Big to Small is : ");
- _Btos(p);
- _show(p,s);
- free(p);
- }
复制代码
|
|