记录一下现在写的代码,以后深造或者工作了还能翻出来拷打一下之前的自己。
5807 读一个字符串,大写变小写。
#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]>=65&&array[i]<=90)
{
array[i]+=32;
}
i++;
if(i==LEN-1)
{
break;
}
//end
}
printf("%s",array);
return 0;
}
5813 碱基序列配对(A-T、G-C)
#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';
}
else if(one[i]=='T')
{
the_other[j]='A';
}
else if(one[i]=='C')
{
the_other[j]='G';
}
else
{
the_other[j]='C';
}
i+=1;
j+=1;
if(j==LEN)
{
break;
}
//end
}
the_other[j] = '\0';
puts(the_other);
return 0;
}
5810 数据压缩存储 输入以0开头的二进制0-1代码,输出每个的个数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 100
int main()
{
int compress(char array[], int count[]);
char array[LEN];
int count[LEN]={0};
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]);
}
return 0;
}
int compress(char array[], int count[])
{
//start
int num=0,i,n=strlen(array),j=0;
for(i=0;i<n;i++){
if(array[i]==array[i+1]){
j++;
}
else{
count[num++]=j+1;
j=0;
}
}
return num;
//end
}
5811 凯撒加密 字母表里移动,超出范围要修改
#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]>=65&&message[i]<=90)
{
if((message[i]+shift)>90)
{
message[i]=message[i]+shift-90+64;
}
else
{
message[i]=message[i]+shift;
}
}
else if(message[i]>=97&&message[i]<=122)
{
if((message[i]+shift)>122)
{
message[i]=message[i]+shift-122+96;
}
else
{
message[i]=message[i]+shift;
}
}
}
//end
}
5812 颠倒句子中的单词的顺序(这题写拉了...)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
char tmp[100];
char *t[100];
printf("Enter a sentence:");
gets(str);
int i,j=0;
int k=0;
int w;
for(i=0;str[i]!='.'&&str[i]!='?'&&str[i]!='!';i++)
{
if(str[i]!=' ')
{
tmp[j]=str[i];
j++;
}
else
{
tmp[j]='\0';
if(strlen(tmp)!=0)
{
t[k]=(char *)malloc(j*sizeof(char));
strcpy(t[k],tmp);
k++;
j=0;
}
}
}
char c=str[i];
tmp[j]='\0';
if(strlen(tmp)!=0)
{
t[k]=(char *)malloc(j*sizeof(char));
strcpy(t[k],tmp);
k++;
}
printf(" Reversal of sentence:");
printf(" ");
for(i=k-1;i>0;i--)
{
printf("%s ",t[i]);
}
printf("%s",t[0]);
printf("%c",c);
return 0;
}