输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
#include <stdio.h>
int main()
{
char *str[] = {“head”, “tail”};
char p[100];
printf ("请输入一串字符串( < 100)\n");
fgets (p, 100, stdin);
int i = 0;
int j = 0;
int index = 0; // i的下标
char *p1 = p;
int m = 0;
int flag = 0, s = 0;// 标志位
for (; i < 100; i++ )
{
while (j < 4)
{
if (p[i] == str[m][j])
{
flag = 1;
s = m;
i++;
j++;
}
else
{
s = 0;
flag = 0;
break;
}
}
if (s == 1 && flag == 1)
{
p[i] = '\0';
break;
}
if (flag == 1 && m == 0)
{
p1 = &p[i - j];
m = m + 1;
j = 0;
}
}
printf ("%s\n", p1);
return 0;
}
请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
#include <stdio.h>
int main()
{
int num, n, m;
printf ("请输入一个数(10进制)\n");
scanf ("%d", &num);
printf ("请输入要转换的进制\n");
scanf ("%d", &n);
char s[100];
int i = 0;
int index = 0;
m = num;
if (m < 0){
i = 1;
index = i;
s[0] = '-';
m = -m;
}
while (m && n != 16)
{
s[i] = m % n + '0';
i++;
m /= n;
}
if (n != 16)
s[i] = '\0';
if (n == 16){
while (m )
{
switch (m % 16)
{
case 1: s[i++] = '1'; break;
case 2: s[i++] = '2'; break;
case 3: s[i++] = '3'; break;
case 4: s[i++] = '4'; break;
case 5: s[i++] = '5'; break;
case 6: s[i++] = '6'; break;
case 7: s[i++] = '7'; break;
case 8: s[i++] = '8'; break;
case 9: s[i++] = '9'; break;
case 10: s[i++] = 'A'; break;
case 11: s[i++] = 'B'; break;
case 12: s[i++] = 'C'; break;
case 13: s[i++] = 'D'; break;
case 14: s[i++] = 'E'; break;
case 15: s[i++] = 'F'; break;
case 0: s[i++] = '0'; break;
defult: break;
}
m /= 16;
}
s[i] = '\0';
}
int right = i - 1;
int left = index;
while(left < right)
{
int t = s[left];
s[left++] = s[right];
s[right--] = t;
}
printf ("%s\n", s);
return 0;
}
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
#include <stdio.h>
int len(char *str);
void reverse(char *str)
{
int l = len(str);
int left = 0;
int right = l - 1;
while (left < right)
{
int t = str[left];
str[left++] = str[right];
str[right--] = t;
}
}
int len(char *str)
{
int count = 0;
while (*str)
{
str++;
count++;
}
return count;
}
int main()
{
char str[100] = “I am a student”;
reverse(str);
char *p1 = str;
char *p2 = str;
while (*p2)
{
if (*p2 == ' ')
{
*p2 = '\0';
reverse(p1);
p1 = p2 + 1;
*p2 = ' ';
}
p2++;
}
reverse(p1);
printf ("%s\n",str);
return 0;
}