2020年2月29日 字符串
从3月2日开始,新的学期就开始了,除了上ACM的选修课之外,之后做题就以锐格为主了,锐格上只能用C语言,所以有些题会有点挑战性。所有未解出的题,我也尽快解决后完善。
2.29 课程内实验>实验1:字符串(5/5)
题目并不难,但很多题都是补充代码的题,这意味者读懂已有的代码、了解已有代码的含义,然后题目就会很简单。
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]>='A' && array[i]<='Z')
array[i]+=32;
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[i]='T';
if(one[i]=='T')
the_other[i]='A';
if(one[i]=='G')
the_other[i]='C';
if(one[i]=='C')
the_other[i]='G';
i++;
j++;
//end
}
the_other[j] = '\0';
puts(the_other);
return 0;
}
5810
上学期的题目,这次要求用函数解决,把旧版的操作转移到函数中即可。
#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;
continue;
}
}
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,n=strlen(message);
for(i=0;i<n;i++){
if(message[i]>='A'&&message[i]<='Z')
message[i]=(message[i]-'A'+shift)%26+'A';
if(message[i]>='a'&&message[i]<='z')
message[i]=(message[i]-'a'+shift)%26+'a';
}
//end
}
5812
未解题目,开头的空格实属闸总,不知道从哪儿冒出来的空格,直接错误。
以下代码为WA代码,等待研究中。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char s[101];
void reverse(int begin,int end)
{
while(begin<end){
char tmp=s[begin];
s[begin]=s[end];
s[end]=tmp;
begin++;
end--;
}
}
int main()
{
void reverse(int begin,int end);
printf("Enter a sentence: ");
gets(s);
printf("Reversal of sentence:");
int i,n=strlen(s),begin=0;
char c=s[n-1];
reverse(0,n-2);
for(i=0;i<n;i++){
if(s[i]!=' '){
if(i>0&&s[i-1]==' ')
begin=i;
else if(i==n-1)
reverse(begin,i-1);
else if(i<n-1&&s[i+1]==' ')
reverse(begin,i);
}
}
printf("%s",s);
return 0;
}
解题成功 更新时间2020.4.2 思路来自老师。。
只要不是空格或其他特殊符号就直接输出 否则跳过。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char isalpha(char a)
{
if(a>='A' && a<='Z' || a>='a' && a<='z' || a=='\'')
return 1;
else
return 0;
}
int main()
{
char a[100],c;
int m,i,n;
printf("Enter a sentence: ");
while(gets(a))
{
n=strlen(a);
m=0;
for(i=0;i<n;i++)
{
if(a[i]=='.' || a[i]=='?' || a[i]=='!')
{
c=a[i];
if(isalpha(a[i-1])) m++;
a[i]='\0';
break;
}
else if(isalpha(a[i])==0)
{
if( isalpha(a[i-1]) ) m++;
a[i]='\0';
}
}
printf("Reversal of sentence: ");
for(;i>=0 && m>=0;i--)
{
if(i==0 && a[i]!='\0' || i>0 && a[i]!='\0' && a[i-1]=='\0')
{
if(m>1) printf("%s ",&a[i]);
else if(m==1) printf("%s",&a[i]);
m--;
}
}
printf("%c\n",c);
}
return 0;
}