1. 题目: 请编写一个C函数,该函数将一个字符串逆序
#include <stdio.h>
#include <string.h>
char* reverse(char *x)
{
int len = strlen(x);
int i, n = len / 2;
char t;
for (i = 0; i <= n; i++)
{
t = x[i];
x[i] = x[len - 1 - i];
x[len - 1 - i] = t;
}
return x;
}
int main()
{
char str[100];
printf("input a string :");
gets(str);
puts(reverse(str));
return 0;
}
2. 题目: 请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
#include <stdio.h>
int fun2(int num);
int fun16(int num);
int fun8(int num);
int main()
{
int num,kind; //kind是用户需要变换的进制
printf("Enter the number : \n");
scanf("%d",&num);
printf("what kind of number do you want to change(2 or 16 or 8) :\n");
scanf("%d",&kind);
if(kind == 2)
{
fun2(num);
}
else if(kind == 16)
{
fun16(num);
}
else if(kind == 8)
{
fun8(num);
}
else
{
printf("error!\n");
}
return 0;
}
int fun2(int num) //二进制转换
{
int s[32] = {0};
int i,n = 0;
while(num != 0)
{
s[n++] = num % 2;
num = num / 2;
}
printf("二进制数为 :");
for(i = n - 1;i >= 0;i--)
{
printf("%d",s[i]);
}
printf("\n");
return 0;
}
int fun16(int num) //十六进制转换
{
int s[32] = {0};
int i,n = 0;
while(num != 0)
{
s[n++] = num % 16;
num = num / 16;
}
printf("十六进制数为 :");
for(i = n - 1;i >= 0;i--)
{
printf("%d",s[i]);
}
printf("\n");
return 0;
}
int fun8(int num) //八进制转换
{
int s[32] = {0};
int i,n = 0;
while(num != 0)
{
s[n++] = num % 8;
num = num / 8;
}
printf("八进制数为 :");
for(i = n - 1;i >= 0;i--)
{
printf("%d",s[i]);
}
printf("\n");
return 0;
}
3. 题目: 输入一个字符串,计算字符串中子串出现的次字数
#include <stdio.h>
#include <string.h>
void main()
{
char str1[20], str2[20], *p1, *p2;
int sum=0;
printf("Please enter two strings\n");
scanf("%s%s", str1, str2);
p1=str1;
p2=str2;
while(*p1 != '\0')
{
if(*p1 == *p2)
{
while(*p1 == *p2 && *p2 != '\0')
{
p1++;
p2++;
}
}
else
p1++;
if(*p2 == '\0')
sum++;
p2=str2;
}
printf("%d", sum);
getchar();
}
4. 题目: 编写一个C函数,将”I am from shanghai ”倒置为”shanghai from am I”,即将句子中的单词位置倒置,而不改变单词内部结构.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int i=0;
char str[1000]={0};
char ch,*p=str;
while((ch=getchar())!='\n')
{
str[i]=ch;
i++;
}
while(*p!='\0')
{
if(*p==' ')
{
*p='\0';
}
p++;
}
while(p!=str)
{
p--;
if(*p=='\0')
{
printf("%s ",p+1);
}
}
printf("%s",p);
while(1);
return 0;
}
5. 题目: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
提示:帧头和帧尾分别是head和tail 字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧
#include <stdio.h>
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;
}