1.请编写一个C函数,该函数将一个字符串逆序
#include
#include
char *reserve(char *s)
{
int i;
int n = strlen(s)/2;
char tmp;
for(i = 0; i <= n; i++)
{
tmp = s[i];
s[i] = s[strlen(s)-i-1];
s[strlen(s)-i-1] = tmp;
}
return s;
}
int main()
{
char str[20];
printf("Please input a string:\n");
scanf("%s", str);
printf("\nThe reserved string is:\n%s\n", reserve(str));
return 0;
}
2.请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
#include
void Transfer(int num, int radix, int *Arr)
{
int i = 0;
int temp = 0;
if (radix == 2)
{
while (num)
{
Arr[i++] = num % 2;
num /= 2;
}
printf ("num = ");
while (i)
{
printf ("%d", Arr[--i]);
}
printf ("B\n");
}
else if (radix == 8)
{
while (num)
{
Arr[i++] = num % 8;
num /= 8;
}
printf ("num = ");
while (i)
{
printf ("%d",Arr[--i]);
}
printf ("O\n");
}
else if (radix == 16)
{
while (num)
{
temp = num % 16;
switch(temp)
{
case 1:Arr[i++] = '1';break;
case 2:Arr[i++] = '2';break;
case 3:Arr[i++] = '3';break;
case 4:Arr[i++] = '4';break;
case 5:Arr[i++] = '5';break;
case 6:Arr[i++] = '6';break;
case 7:Arr[i++] = '7';break;
case 8:Arr[i++] = '8';break;
case 9:Arr[i++] = '9';break;
case 10:Arr[i++] = 'A';break;
case 11:Arr[i++] = 'B';break;
case 12:Arr[i++] = 'C';break;
case 13:Arr[i++] = 'D';break;
case 14:Arr[i++] = 'E';break;
case 15:Arr[i++] = 'F';break;
default:printf ("error!\n");
}
num /= 16;
}
while (i)
{
printf ("%c", Arr[--i]);
}
printf ("H\n");
}
}
int main()
{
int num = 0;
int radix = 0;
int Arr[100] = {0};
printf ("Please input the number : ");
scanf ("%d", &num);
printf ("Please input the radix : ");
scanf ("%d", &radix);
Transfer(num, radix, Arr);
return 0;
}
3.输入一个字符串,计算字符串中子串出现的次字数
#include
#include
int main()
{
char a[100];
printf("请输入字符串\n");
scanf("%s",a);
char * c = a;
char b[10];
printf("请输入子串\n");
scanf("%s",b);
char * d = b;
int n;
int num = 0;
n = strlen(d);
while(strlen(c) > 0)
{
if(strncmp(c,d,n) == 0)
{
num++;
c += n;
}
else
{
c++;
}
}
printf("%d\n",num);
return 0;
}
4.编写一个C函数,将”I am from shanghai ”倒置为”shanghai from am I”,即将句子中的单词位置倒置,而不改变单词内部结构
#include
#include
#include
int main()
{
char *str[] = { "I", "am","from","shanghai" ,NULL};
int i = 0;
int j = 0;
int len = 0;
char *temp;
while (str[i] != NULL)
{
i++;
len++;
}
for(i = 0, j = len - 1; j > i; i++,j--)
{
temp =*(str+i);
*(str+i) = *(str+j);
*(str+j) = temp;
}
for(i = 0; i < len; i++)
{
printf("%s ", *(str+i));
}
printf("\n");
return 0;
}
5.输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来
#include
int find(char *s,char *a,char *b);
int main()
{
char s[100],a[100],b[100];
printf("Enter the long string : \n");
scanf("%s",s);
printf("Enter the head string : \n");
scanf("%s",a);
printf("Enter the tail string : \n");
scanf("%s",b);
find(s,a,b);
return 0;
}
int find(char *s,char *a,char *b)
{
int head,tail;
int i = 0,j = 0,k = 0,flag = 0;
while(s[i] != '\0')
{
if((s[i] == a[j]) && !flag)
{
head = i;
while((s[i] == a[j]) && s[i] != '\0')
{
i++;
j++;
flag = 1;
}
}
if((a[j] == '\0') && flag)
{
while((s[i] == b[k]) && s[i] != '\0')
{
i++;
k++;
flag = 0;
}
}
if((b[k] == '\0') && !flag)
{
tail = i;
for(i = head;i < tail;i++)
{
printf("%c",s[i]);
}
printf("\n");
break;
}
i++;
}
return 0;
}