锐格实验——字符串

 

目录

基础模型

5766  字符插入并排序

5767  字符替换

5786  字符删除

5794  字符串拷贝

5796  字符串的连接

5795  字符串比大小

7124  字符串出现次数

7089   字符串中字母及个数

 

 

5810  数据压缩储存

5582  整数逆序数

5812  句子单词逆序输出

7165  句子中最长单词

5807  大写变小写​

5813  碱基序列配对

8511  凯撒加密

5635  回文数字

5578  判断一个是另一个的倍数

5715  折半查找

5789  把前m个数放后面

5790  交换数组中的最大最小值

5791  冒泡排序

5432  求最小公倍数 

5387  求最大公约数

5355  剪刀石头布

7175  (万能)十进制变十八进制


  • 以下顺序较乱,建议通过目录查找

5807   大写变小写

ps.比较简单的一道,字符串循环。

#include <stdio.h>
#include <stdlib.h>
#define LEN 10
int main()
{
   char array[LEN];
   int i;
   gets(array);
   i=0;
   while(array[i]!='\0')
   {
      //start
       if(array[i]>='A'&&array[i]<='Z')
      {
          array[i]=array[i]+32;
          i++;
      }
      else
      i++;
      //end
   }
   printf("%s",array);
   return 0;
}

 


5813   碱基序列配对

#include <stdio.h>
#include <stdlib.h>
#define LEN 100
int main(void)
{
    char one[LEN],the_other[LEN];  //one用于存储原串;the_other用于存储匹配串
    int i,j;
    gets(one);
    i=0, j=0;
    while(one[i]!='\0')
    {
       //start
       if(one[i]=='A')
          the_other[j]='T';
       if(one[i]=='T')
          the_other[j]='A';
       if(one[i]=='C')
          the_other[j]='G';
       if(one[i]=='G')
          the_other[j]='C';

        i++;j++;
       //end
    }
    the_other[j] = '\0';
    puts(the_other);
    return 0;
}

 


5810  数据压缩储存

 ps.这道比较难处理(若有更好方法欢迎指导)

#include <stdio.h>
#include <stdlib.h>
#define LEN 100
int main()
{
   int compress(char array[], int count[]);
   char array[LEN];
   int count[LEN];
   int i;
   int tail; //count数组的有效最末下标
  while(scanf("%s",array)!=-1)
   {
      tail = compress(array, count);   //引用函数
      for(i=0;i<tail;i++)
        i<tail-1 ? printf("%d ",count[i]) : printf("%d\n",count[i]);   
     //是否小于tail-1,如果是。。。。。。。。。如果不是(最后一行)。。。。。
   }
   return 0;
}
int compress(char array[], int count[])
{
//start
int i,j=0,sum=0,s=0;
   for(i=1;array[i]!='\0';i++)
   {
       if(array[i-1]==array[i])         //从前往后一个一个比
       {sum++;count[j]=sum;}           //若相等,sum+1,用count随时记录个数
       else
       {count[j]=sum+1;sum=0;s++;j++;}  //若不相等,count+1,sum归0
   }                                    //j++(开始下一轮count的循环)s++(记录输出个数)

   if(array[i-1]==array[i-2])           //注意处理最后一个数字
      count[j]=sum+1;                   //相等,sum+1;不相等,count数组里最后再来个1
   else
      count[j]=1;

   return s+1;
//end
}

 


8511 凯撒加密

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 81
int main()
{
   void Caesar_transform(char message[], int shift);
   char message[LEN];
   int shift;
   printf("Enter message to be encrypted: ");
   gets(message);
   printf("Enter shift amount (1-25): ");
   scanf("%d",&shift);
   printf("Encrypted message: ");
   Caesar_transform(message, shift);
   printf("%s\n",message);
   return 0;
}
void Caesar_transform(char message[], int shift)
{
//start
int i;
   for(i=0;message[i]!='\0';i++)
   {
       if(message[i]>='a'&&message[i]<='z')
       message[i]=(message[i]+shift-'a')%26+'a';    //对于'a'先减后加
       if(message[i]>='A'&&message[i]<='Z')
       message[i]=(message[i]+shift-'A')%26+'A';

   }
//end
}

 


5812   单词逆序输出 

ps.比较难也比较复杂! 复制了一下老师的版本,用到“计数器”

#include <string.h>
int main()
{
    char a[100], ch;
    int i = 0, word = 0, f = 1;
    printf("Enter a sentence: ");
    i = 0;
    while (1)//逐个输入
    {
        a[i] = getchar();//把输入的第一个字符放到a[i]里
        if (a[i] == '?' || a[i] == '!' || a[i] == '.')//句末符号,退出判断并记录
        {
            ch = a[i];
            break;
        }
        if (a[i] != ' ' && f == 1)//统计单词数(f=1为“计数器可用”,在开始写入单词后立马f=0,表示目前在写的单词已经计数了,计数器不可用)
        {
            word++;
            f = 0;
        }
        if (a[i] == ' ' && f == 0)//一个单词输入完成(让f=1,表示计数器可用,可以开始下一个单词输入)
            f = 1;
        i++;
    }
    ch = a[i];
    a[i] = '\0';
    printf("Reversal of sentence: ");
    

    /*以下为输出部分*/
    f = 0;//因为最后一个字符非字母,所以f=0不能输出
    while (i != -1)
    {
        if (a[i] == ' ' || a[i] == '\0')
        {
            a[i] = '\0';//这里从后往前把单词单独切开
            if (f == 1)//f=1才能输出
            {
                f = 0;
                if (word > 1)
                    printf("%s ", a + i + 1);
                else
                    printf("%s", a + i + 1);
                word--;
            }
        }
        else//只要碰到字母,就f=1,表示可以输出
        {
            if (f == 0)
                f = 1;
        }
        if (i == 0 && a[i] != '\0')
            printf("%s", a);
        i--;
    }
    printf("%c\n", ch);
    //system("pause");
    return 0;
}

 


5635 回文数字

int main()
{
    int x[10],n,i,k;
    while(scanf("%d",&n)!=-1)
    {    k=0;
         while(n>0)
        {

           x[k]=n%10;
            k++;
            n=n/10;
        }

        for(i=0;i<=(k-1)/2;i++)
        if(x[i]!=x[k-1-i])
        break;
           if(i>(k-1)/2)
             printf("1\n");
             else
            printf("0\n");

   }

    return 0;
}

 


5578  判断一个是另一个的倍数

int main()
{
    int x,y,flag;
    while(scanf("%d%d",&x,&y)!=-1)
    {
       flag=func(x,y);
       printf("%d\n",flag);
    }
    return 0;
}
int func(int x,int y)
{
    if(x>y&&x%y==0)
    return 1;
    else
    return 0;
}

 


5582  整数逆序数

int main()
{
    int x,k;
    while(scanf("%d",&x)!=-1)
    {
        k=func(x);
        printf("%d",k);
    }
    return 0;
}

int func(int x)
{
    int sum,k;
    sum=0;
    while(x>0)
    {
        k=x;
        k=k%10;
        sum=sum*10+k;
        x=x/10;
    }
    return sum;
}

 


5715  折半查找

int main()
{
    int x,y,k,i,a[200];
    int low,high,mid;
    while(scanf("%d %d",&x,&y)!=-1)
    {
        for(i=0;i<x;i++)
        a[i]=i+1;
        low=0;high=x-1;mid=(low+high)/2;k=0;
        while(low<=high)
        {
            if(a[i]==mid)
            {
                k++;
            break;
            }

            if(a[i]>mid)
            {
                low=mid+1;
                k++;
            }

            if(a[i]<mid)
            {
                high=mid-1;
                k++;
            }
            mid=(low+high)/2;

        }
        if(low>high)
        printf("%d\n",0);
        else
        printf("%d\n",k);
    }

    return 0;
}

 


5766  字符插入并排序

int main()
{
    char str[100],ch;
    while(scanf("%s %c",str,&ch)!=-1)
    {
        str_len(str,ch);
        printf("%s\n",str);
    }
    return 0;
}

 void str_len(char a[],char ch)
 {
     int i,j,m;
     char t;
     i=0;
     while(a[i]!='\0')
     i++;

    a[i]=ch;
    for(j=0;j<i;j++)
    for(m=0;m<i-j;m++)
        if(a[m]>a[m+1])
        {    t=a[m];
             a[m]=a[m+1];
             a[m+1]=t;
        }
        
 }

 


5767  字符替换

void change(char a[],char x,char y)
{
    int i=0;
    while(a[i]!='\0')
    {

        if(a[i]==x)
        a[i]=y;
        i++;
    }
}

int main()
{
    char a[100],x,y;
    int i;
    while(scanf("%s %c %c",a,&x,&y)!=-1)
  {
    change(a,x,y);
    printf("%s\n",a);
  }
    return 0;
}

 


5786  字符删除

int main()
{
    char str[100],ch;
    int x;
    while(scanf("%s %c %d",str,&ch,&x)!=-1)
   {
     str_del(str,ch,x);
     printf("%s\n",str);

   }
   
   return 0;
}
void str_del(char str[],char ch,int x)
{
    int i,j;
    i=0;j=0;
    while(str[i]!='\0')
    {
        if(str[i]!=ch)
        {
            str[j]=str[i];
            j++;
        }
        i++;
    }str[j]='\0';

    for(j=0;str[j]!='\0';j++)
    {
       if(str[j]>='a'&&str[j]<='z')
          str[j]=(str[j]-'a'+x)%26+'a';
    }

}

 


5789  把前m个数放后面

void func_xx(int a[],int n)
{
//start
int i,j,temp;
for(i=0;i<n;i++)
{
    temp=a[0];
    for(j=0;j<4;j++)
    a[j]=a[j+1];
    a[j]=temp;
}
//end
}


int main()
{
  int i,n,a[5];
  while(scanf("%d",&n)!=-1)
  {
      for(i=0;i<5;i++)
       scanf("%d",&a[i]);
      func_xx(a,n);
      for(i=0;i<5;i++)
       printf("%d ",a[i]);
      printf("\n");
  }
return 0;
}

 


5790  交换数组中的最大最小值

int main()
{
  int i,m,a[10];
  //start    //end
  while(scanf("%d",&a[0])!=-1)
   {
    for(i=1;i<10;i++)
       scanf("%d",&a[i]);
    func_xx(a,10);
    for(i=0;i<10;i++)
       printf("%d ",a[i]);
    printf("\n");
 }
return 0;
}

void func_xx(int b[],int n)
{
 //start
 int i,temp,max_pos,min_pos;
max_pos=min_pos=0;
for(i=1;i<n;i++)
{if(b[i]>b[max_pos])
max_pos=i;
if(b[i]<b[min_pos])
min_pos=i;}

temp=b[max_pos];
b[max_pos]=b[min_pos];
b[min_pos]=temp;
 //end
}

 


5791  冒泡排序

#include <stdio.h>
#include <stdlib.h>
int main()
{
 double data[10];
 int tmp,i,j,k;
 while(scanf("%lf",&data[0])!=-1)
 {
     for(i=1;i<10;i++)
        scanf("%lf",&data[i]);
     func_sort(data,10);
     for(i=0;i<10;i++)
         printf("%.1lf ",data[i]);
     printf("\n");
 }
    return 0;
}
void func_sort(double a[],int n)
{
    //start
    int i,j;
    double t;
    for(i=0;i<10-1;i++)
    for(j=0;j<10-i-1;j++)
    if(a[j]<a[j+1])
    {
        t=a[j];
        a[j]=a[j+1];
        a[j+1]=t;
    }
    //end
}

 


5794  字符串拷贝

int main()
{
    char str[100],dst[100];
    while(scanf("%s",str)!=-1)
    {
        str_copy(str,dst);
        printf("After Copy:%s\n",dst);
    }
    return 0;
}

void str_copy(char s[],char d[])
{
    int i;
    i=0;
    while(s[i]!='\0')
        {
            d[i]=s[i];
            i++;
        } 
    d[i]='\0';

}

 


5795  字符串比大小

int main()
{
    char str[100],dst[100];
    int k;
    while(scanf("%s %s",str,dst)!=-1)
    {
        k=str_cmp(str,dst);
        printf("%d\n",k);
    }
    return 0;
}
int str_cmp(char a[],char b[])
{
    int m,n,i;
    m=0;
    while(a[m]!='\0')
    m++;
    
    n=0;
    while(a[n]!='\0')
    n++;
    
    i=0;   
    if(m<=n)
    {
        while(a[i]!='\0')
        {
            if(a[i]==b[i])
            i++;
            else
              if(a[i]>b[i])
              return 1;
              else
              return -1;

        }
        return 0;
    }

     if(m>n)
    {
        while(b[i]!='\0')
        {
            if(a[i]==b[i])
            i++;
            else
              if(a[i]>b[i])
              return 1;
              else
              return -1;

        }
        return 0;
    }
}

 


5796  字符串的连接

int main()
{
    char s[100],t[100],q[100];
    while(scanf("%s %s",s,t)!=-1)
    {
        str_cat(s,t,q);
        printf("%s\n",q);
    }
    return 0;
}


void str_cat(char s[],char t[],char q[])
{
    int i,j;

    for(i=0;s[i]!='\0';i++)
    q[i]=s[i];

    for(j=0;t[j]!='\0';j++)
    q[i+j]=t[j];
    q[i+j]='\0';
}

 


7165  句子中最长单词

emm

5432  求最小公倍数 

int main()
{
    int least_common_multiple(int x, int y);
    int x, y, z;
    scanf("%d%d%d",&x,&y,&z);
    printf("%d\n",least_common_multiple(least_common_multiple(x,y),z));
    return 0;
}
int least_common_multiple(int x, int y)
{
    int i,t,k;
    if(x<y)
    {
       t=x;x=y;y=t;
    }

    for(i=x;i>0;i++)
    {
        if(i%x==0&&i%y==0)
         break;
    }
   
  return i;
}

 


5387  求最大公约数

int main()

{
    int x,y,t,i;

    scanf("%d %d",&x,&y);
    if(x>y)
    {
        t=y;
        y=x;
        x=t;
    }
    for(i=x;i>0;i--)
    {
       if(x%i==0&&y%i==0)
       break;
    }
    printf("%d\n",i);
    
  return 0;
}

 

5355  剪刀石头布

#include <stdlib.h>
#include <stdio.h>
int main()
{
    int computer;
    int me;
    scanf("%d",&me);
    //start
    computer=rand()%3+1;
    if(computer-me==0)
    printf("Draw.");
    if(computer-me==1||me-computer==2)
    printf("You win.");
    if(me-computer==1||computer-me==2)
    printf("You lose.");
    
    //end
    return 0;
}

 


7175  (万能)十进制变十八进制

int main()
{
    int x,j,k,m;
    char a[100],c[100];
    char *b="0123456789ABCDEFGH";
    scanf("%d",&x);

    j=0;
    while(x!=0)
    {
        k=x%18;
        a[j]=b[k];
        x=x/18;
        j++;
    }
    a[j]='\0';
    m=j-1;

    j=0;
    while(a[j]!='\0')
    {
        c[m]=a[j];
        m--;
        j++;
    }
    c[j]='\0';
    printf("%s",c);
    return 0;
}

7124  字符串出现次数

输入:ababaabcc

输出:3


int len(char b[])
{
    int i=0;
    while(b[i]!='\0')
    i++;

    return i;
}
int main()
{
    int flag,sum=0,l,i,j,k;
    char a[100],b[100];
    scanf("%s %s",a,b);
    l=len(b);

    for(i=0;a[i]!='\0';i++)
    {
        flag=0;
        k=i;
        if(a[i]==b[0])
        {
            for(j=1;j<l;j++)
            {
                if(a[k+1]==b[j])
                k++;
                else
                flag=1;

            }
         if(flag==0)
            sum++;
        }

    }
    printf("%d\n",sum);
    return 0;
}

7089   字符串中字母及个数

int main()
{
    char str[100];
	int alp[26] = {0};
	scanf("%s", str);
	for (int i = 0; i < strlen(str); i++)
	{
		if (str[i] >= 'a' && str[i] <= 'z')
			alp[str[i] - 'a']++;
	}
	for (int i = 0; i < 26; i++)
		if (alp[i] != 0)
			printf("%c  %d\n", i + 'a', alp[i]);
	return 0;
}

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值