第1题:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *ptr = (char *) malloc(sizeof(char) * 32);
char *begin ;
if(NULL == ptr)
{
printf("malloc is failure!\n");
return -1;
}
printf("input:\n");
scanf("%s", ptr);
begin = ptr;
ptr += strlen(ptr) -1;
while(ptr >= begin)
{
printf("%c", *ptr--);
}
while(1);
return 0;
}
第2题:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void change(int a,char *s,int n)
{
int i,j=0;
for(i=0;i<32;i++)
{
s[i]=a%n;
a=a/n;
if(a==0)
{
break;
}
}
for(;i>=0;i--,j=0)
{
if(s[i]>=10)
{
switch(s[i])
{
case 10:s[i]='A';printf("%c",s[i]);j++;break;
case 11:s[i]='B';printf("%c",s[i]);j++;break;
case 12:s[i]='C';printf("%c",s[i]);j++;break;
case 13:s[i]='D';printf("%c",s[i]);j++;break;
case 14:s[i]='E';printf("%c",s[i]);j++;break;
case 15:s[i]='F';printf("%c",s[i]);j++;break;
default :printf("大于16进制只用数字表示\n");break;
}
}
if((i=i-j)>=0)
{
printf("%d",s[i]);
}
}
}
int main()
{
int a,n;
char *s=(char*)malloc(sizeof(char)*64);
printf("输入一个整数\n");
scanf("%d",&a);
printf("输入转换成几进制数\n");
scanf("%d",&n);
change(a,s,n);
printf("\n");
while(1);
return 0;
}
第3题:
#include<stdio.h>
#include<string.h>
int fun(char *a, char *b)
{
int len_b;
int count = 0;
int num = 0;
char *temp = b;
len_b = strlen(b);
while (*a != '\0')
{
if (*a == *temp)
{
while ((*a == *temp) && (*a != '\0') && (*temp != '\0'))
{
num++;
a++;
temp++;
}
if (num == len_b)
{
count++;
}
num = 0;
temp = b;
}
else
{
a++;
}
}
return count;
}
int main()
{
char a[100];
char b[10];
int num;
printf("Input the main string\n");
scanf("%s",a);
printf("Input the substring\n");
scanf("%s",b);
num = fun(a,b);
printf("The number is:%d\n",num);
while(1);
return 0;
}
第4题:
#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\n", p);
while (1);
return 0;
}
第5题:
#include <stdio.h>
int mystrlen(char *str)
{
int length = 0;
while (*str++)
{
length++;
}
return length;
}
char* mystrstr(char *str, 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 :\n%s\n", outbuf);
while(1);
return 0;
}