苏嵌 项目实训 学习日志 鲍玉华 2018.7.15

6.

 

#include <stdio.h>

 

#include <string.h>

 

#include <stdio.h>

 

//字符串逆序

 

int main()

 

{

 

char *ptr = (char *)malloc(sizeof(char) * 64);

 

char *begin;

 

    if(NULL==ptr)

 

{

 

printf("malloc 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;

 

}

 

 

 

7.

 

#include <stdio.h>

 

 

 

void change(int num,int n);

 

 

 

int main()

 

{

 

        int num,n;

 

 

 

        printf("Please enter a number to change:");

 

        scanf("%d",&num);

 

        printf("请输入要转换的进制(如2或8,16等):");

 

        scanf("%d",&n);

 

 

 

        change(num,n);

 

 

 

        printf("\n");

 

    return 0;

 

}

 

 

 

void change(int num,int n)            //利用递归函数的实现反序输出

 

{

 

        int r;

 

 

 

        r = num % n;

 

        if(num > 0)

 

                change( num / n,n );

 

        putchar( '0' + r);

 

 

 

    }

 

 

 

8.

 

#include <stdio.h>

 

#include <string.h>

 

 

 

// 从str1中查找str2的个数,并返回

 

int findChildCnt(char* str1, char* str2)

 

{

 

    int len = strlen(str2);

 

    int cnt = 0;

 

    while (str1 = strstr(str1, str2)) // 如果查找到,则执行循环,否则为空退出循环

 

    {

 

        cnt++; // 统计次数

 

        str1 += len; // 加上偏移量,即移除str2

 

    }

 

    return cnt;

 

}

 

 

 

int main()

 

{

 

    char str1[100], str2[100];

 

    printf("intput str1 :");

 

    gets(str1);

 

    printf("intput str2 :");

 

    gets(str2);

 

    printf("Child Cnt: %d\n", findChildCnt(str1, str2));

 

    return 0;

 

}

 

 

 

 

 

9.

 

#include<stdio.h>

 

#include<stdlib.h>

 

#include<string.h>

 

#define SIZE 4

 

int main()

 

{

 

int i;

 

char t[20];

 

char *str[SIZE]={0};

 

printf("input:\n");

 

    for(i=0;i<SIZE;i++)

 

{

 

str[i]=(char*)malloc(sizeof(char)*30);

 

    if(NULL==str[i])

 

{

 

printf("malloc failure!\n");

 

     return(-1);

 

}

 

scanf("%s",str[i]);

 

}    

 

for(i=0;i<SIZE/2;i++)

 

{

 

       strcpy(t,str[i]);

 

   strcpy(str[i],str[SIZE-i-1]);

 

       strcpy(str[SIZE-i-1],t);

 

}

 

printf("the last string:\n");

 

    for(i=0;i<SIZE;i++)

 

{

 

printf("%-10s",str[i]);

 

}

 

while(1);

 

return 0;

 

}

 

 

 

 

 

10.

 

#include<stdio.h>

 

#include<string.h>

 

 

 

int head_flog = 0;//用于标记找到尾的标志

 

int tail_flog = 0;//用于标记找到头的标志

 

 

 

char *tail(char *s, char *b)//找到tail地址

 

{

 

int num = 0;//计数字符长度,和b比较

 

int len_b;

 

char *temp = b;//用于返回尾地址值的指针

 

 

 

len_b = strlen(b);//b的长度

 

 

 

if((NULL == s) || (NULL == b))//s,b为空,返回空地址

 

{

 

return NULL;

 

}

 

 

 

if(strlen(s) < strlen(b) )//s长度小于b长度,返回空地址

 

{

 

return NULL;

 

}

 

if(head_flog != 0)//找到头后,在开始找尾

 

{

 

while((*s != '\0') && (!tail_flog))//*s不为空,且未找到尾

 

{

 

while(*s == *temp)

 

{

 

s++;

 

    temp++;

 

    num++;

 

    if(num == len_b)//找到尾,赋值尾地址值和找到尾的标记

 

    {

 

tail_flog = 1;

 

temp = s -1;

 

    break;

 

    }

 

}

 

if((num != 0)&&(num != len_b))//伪尾部,重新赋值找

 

{

 

num = 0;

 

temp = b;

 

s--;//若伪尾部和真尾连续,则s指针要后退

 

}

 

s++;

 

}

 

}

 

if(tail_flog != 0)//找到尾,返回尾地址值

 

{

 

return temp;

 

}

 

else

 

{

 

printf("string illgeal!\n");

 

return NULL;

 

}

 

}

 

 

 

char *head(char *s, char *a)//找到head的地址

 

{

 

int num = 0;//计数字符长度,用于和a长度比较

 

int len_a;

 

char *temp = a;//用于返回头地址值的指针

 

 

 

len_a = strlen(a);//字符a长度

 

 

 

if ((NULL == s) ||(NULL == a))//s,a为空,返回空指针

 

{

 

return NULL;

 

}

 

 

 

if (strlen(s) < strlen(a))//s长度小于a的长度,返回空指针

 

{

 

return NULL;

 

}

 

 

 

while ((*s != '\0') && (!head_flog))//*s不为空,且未找到头

 

{

 

while(*s == *temp)

 

{

 

s++;

 

temp++;

 

num++;

 

if(num == len_a)//找到头部,赋值头指针值和找到头的标记

 

{

 

head_flog = 1;

 

temp = s - len_a;

 

break;

 

}

 

}

 

if((num != 0)&&(num != len_a))//伪头部,重新赋值找

 

{

 

num = 0;

 

temp = a;

 

s--;//若伪头和真头连续,则s要指针后退

 

}

 

s++;

 

}

 

 

 

if(head_flog != 0)//找到头,返回头地址值

 

{

 

return temp;

 

}

 

else

 

{

 

printf("string illgeal!\n");

 

}

 

}

 

 

 

int main()

 

{

 

char s[100];

 

char a[5];

 

char b[5];

 

int n;//保存有效字符的长度

 

int i;

 

char *start = NULL;

 

char *end = NULL;

 

 

 

printf("Enter string(<=100)!\n");

 

scanf("%s",s);

 

printf("Enter head string(<=5)!\n");

 

scanf("%s",a);

 

printf("Enter tail string(<=5)!\n");

 

scanf("%s",b);

 

 

 

start = head(s,a);//开始地址

 

end = tail(s,b);//结束地址

 

n = end - start + 1;

 

 

 

if (n > 0)//tail在head的后面,存在有效数据,否则无效

 

{

 

for (i = 0; i < n; i++)

 

{

 

printf("%c",*start);

 

start++;

 

}

 

printf("\n");

 

}

 

else

 

{

 

printf("No data!\n");

 

}

 

 

 

return 0;

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值