19课 字符串查找及大小写转换作业
本帖最后由 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 %din 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;
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);
}
页:
[1]