2014年05月14日

来源:互联网 发布:呱呱社区软件 编辑:程序博客网 时间:2024/04/25 09:30

practice 2-4:restart compile functionsqueeze(s1,s2),delete any match characters between string s1 ands2.
#include
#define MAXLINE 1000
void squeeze(char s1[],char s2[]);
void show(char s[]);
main()
{
 char str1[MAXLINE],str2[MAXLINE];
 gets(str1);
 gets(str2);
 squeeze(str1,str2);
 show(str1);
 return 0;
}
void squeeze(char s1[],char s2[])
{
 int i,j;

 for(i=0;s1[i]!='\0';i++)
  for(j=0;s2[j]!='\0';j++)
  {
   if(s1[i]==s2[j])
    s1[i]='0';
  }

}
void show(char s[])
{
 int i;
 for(i=0;s[i]!='\0';i++)
  if(s[i]!='0')
   putchar(s[i]);
}


practice 2-5:write a function any(s1,s2),compare string s1and s2,find out the position of any words of string s2 come out atthe first time in the string s1,and take it as a result output,ifnone of those was found,return -1
#include
#define MAXLINE 100
int any(char s1[],char s2[]);

main()
{
 char s1[MAXLINE],s2[MAXLINE];
 gets(s1);
 gets(s2);

 any(s1,s2);
 return 0;
}
int any(char s1[],char s2[])
{
 int i,j,flag=0;
 int num=0;


 for(i=0;s2[i]!='\0';i++)
  for(j=i+1;s2[j]!='\0';j++)
  {
   if(s2[j]==s2[i])
    s2[j]=0;
  }
 for(i=0;s2[i]!='\0';i++)
  if(s2[i]==0)
   s2[i]=s2[i+1];
 for(i=0;s2[i]!='\0';i++)
 {
  for(j=0;s1[j]!='\0';j++)
  {
   if(s1[j]==s2[i])
   {
    flag=1;
    ++num;
    break;
   }
  }
  if(flag==1)
  {
   flag=0;
   printf("%c\t%d\n",s2[i],j+1);
   
  }
 }
 
 if(num==0)
  printf("%d",-1);
 return 0;
}
practice 2-10 write a function lower(),transform capitalletter into lowercase.use condition expression like ? : tocompile.
#include

void lower(char c)
{
 printf("%c",(c>='a'&&c<='z')?(c+'A'-'a'):c);
 
}
main()
{
 int c;
 while((c=getchar())!=EOF)
 {
  lower(c);
 }
 return 0;
}

0 0