1.请编写一个将字符串逆序输出的程序
#include <stdio.h>
int main()
{
int i=0,length=0;
char a[100];
char tmp;
printf("请输入一个字符串:\n");
gets(a);
tmp=a[0];
while(tmp!=0)
{
length++;
tmp=a[i++];
}
length--;
for(i=0;i<length/2;i++)
{
tmp=a[i];
a[i]=a[length-i-1];
a[length-i-1]=tmp;
}
puts(a);
while (1);
return 0;
}
2.
请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
#include <stdio.h>
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);
while(1);
return 0;
}
3.输入一个字符串,计算字符串中子串出现的次字数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int i=0,j=0,m=0;
int n;
char str1[100];
char str2[100];
char *p1=str1;
char *p2=str2;
printf("请输入一个字符串:\n");
gets(str1);
printf("请输入子串:\n");
gets(str2);
while(*p1!=0)
{
if(*p1=*p2&&*p2!=0)
{
*p1=str1[i++];
*p2=str2[j++];
}
else
{
*p1=str1[i++];
}
if(*p2!=0)
{
m++;
*p2=str2[0];
}
}
printf("%d",m);
while (1);
return 0;
}
4.编写一个C函数,将”I am from shanghai ”倒置为”shanghai from am I”,即将句子中的单词位置倒置,而不改变单词内部结构.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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");
while(1);
return 0;
}
5. 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.提示:帧头和帧尾分别是head和tail 字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧
#include <stdio.h>
int mystrlen(const char *str)
{
int length = 0;
while (*str++)
{
length++;
}
return length;
}
char* mystrstr(const char *str, const char *sub)
{
int n = 0;
if (sub != NULL)
{
while (*str)
{
for (n = 0; (*(str + n) == *(sub + n)); n++)
{
if (*(sub + n + 1) == '\0')
{
return (char *)str;
}
}
str++;
}
return NULL;
}
else
{
return (char *)str;
}
}
void IdenStr(char *str, char *outbuf)
{
int count = 0;
char *p1 = NULL;
char *p2 = NULL;
char *h = "head";
char *t = "tail";
int tlen = mystrlen(t);
p1 = mystrstr(str, h);
p2 = mystrstr(str, t);
p2 += tlen;
count = p2 - p1;
while (count)
{
*outbuf++ = *p1++;
count--;
}
}
int main()
{
char str[100] = {0};
char outbuf[100] = {0};
printf ("Please input a string (head...tail): \n");
scanf ("%s", str);
IdenStr(str, outbuf);
printf ("The result is %s\n", outbuf);
while(1);
return 0;
}