实训第七天

                                                                         姓名:祖东科                    日期:2018.7.15

作业

 Training4:字符串训练
1.题目: 请编写一个C函数,该函数将一个字符串逆序 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *begin;
char *str=(char*)malloc(sizeof(char) * 32);
char *ptr=(char*)malloc(sizeof(char) * 32);
if(NULL==ptr)


{
printf("malloc failure!\n");
return -1;


}
scanf("%s",ptr);
begin=ptr;
ptr+=strlen(ptr)-1;
while(ptr>=begin)
{
*str++=*ptr--;


}
*str='\0';
printf("%s\n",str-strlen(begin));
while(1);

return 0;
}
2.题目: 请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
#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>


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;
}
}


int Substring(char *str, const char *sub)
{
char *p = str;
int count = 0;
int sublen = mystrlen(sub);


p = mystrstr(p, sub); //在主串中寻找字串


while (p != NULL)
{
count++;   //找到就计数器加1,让主串指针指向该字串之后一位继续寻找
p += sublen;
p = mystrstr(p, sub);
}


return count;
}


int main()
{
char str[100] = {0};
char sub[100] = {0};
int count = 0;


printf ("Please input a main string : \n"); //主串输入
scanf ("%s", str);
getchar();
printf ("Please input a sub string : \n"); //字串输入
scanf ("%s", sub);


count = Substring(str, sub);
printf ("count = %d\n", count); //打印结果
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()
{
int i=0;
char str[100]={0};//用于存放输入的语句
char ch,*p=str;//p指向数组的第一个元素
//每次循环获取一个字符(包括空格),并保存在数组中
while((ch=getchar())!='\n')
{
str[i]=ch;
i++;
}
//把指针p移动到数组的结尾,遇到空格,替换成‘\0’
while(*p!='\0')
{
if(*p==' ')
{
*p='\0';//替换
}
p++;
}
//指针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 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); //找到head,tail的位置
p2 = mystrstr(str, t);
p2 += tlen;


count = p2 - p1; //求出head,tail之间有多少字符
while (count)
{
*outbuf++ = *p1++; //全部存入outbuf中
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值