11.18习题

1、写一函数int fun(char *p)判断一字符串是否为回文,是返回1,不是返回0,出错返回-1.(例如:字符串”123454321”就是回文字符串)

#include <stdio.h>
int fun(char *s) 
{
    char *p;
    p = s;
    while(*p != 0)
    {
        p++;//p指向最后结束标志符号退出 
    }
    p--;//p指向最后一个有效字符
    while(*s == *p && *s != 0)//指向结束标志时退出 
    {
        s++;
        p--;        
    }
    if(*s == '\0')
        return 1;
    else if(*s != 0)
        return 0;
    else
        return -1;
}
int main(void)
{
    char *s ="abab";
    int i;
    i = fun(s);
    if(i == 1)
        printf("是回文\n");
    else if(i == 0)
        printf("不是回文\n");
    else
        printf("出错\n");  
}

2、假设现有一个单向的链表,但是只知道只有一个指向该节点的指针p,并且假设这个节点不是尾节点,试编程实现删除此节点。
节点结构:struct node
{
int data;
struct node *p_next;
};

//设置一个新的结点指针,指向待删结点的下一个。
//交换两个结点的数据域。
//删除待删结点的下一个。
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct node{

    int data;

    struct node *next;

}ElemSN;
ElemSN *CreateLink(int a[],int n){
    ElemSN *h,*tail,*p;
    h=tail=(ElemSN *)malloc(sizeof(ElemSN));
    h->data=a[0];
    h->next=NULL;
    for(int i=1;i<n;i++)
    {
       p = (ElemSN *)malloc(sizeof(ElemSN));
       p->data = a[i];//创建链表
        p->next = NULL;
        tail->next = p;
        tail = p;
    }
    return h;

}
void PrintfLink(ElemSN *h){
    ElemSN *p;
    p=h;
    while(p){
        printf("%5d",p->data);//打印结点
        p=p->next;
    }
}
void del(ElemSN *p){
    ElemSN *s;
    ElemSN s1;
    s = p->next;
    s1.data = s->data;
    s->data = p->data;//交换两个节点的数据域
    p->data = s1.data;
    p->next = p->next->next;//指向p结点的后两个结点
}
int main(void)
 { 
    int a[8]={1,1,2,2,5,5,6,6};
    int key;
    ElemSN *h=NULL;
    ElemSN *p=NULL;
    printf("please Enter the key :");
    scanf("%d",&key); 
    h=CreateLink(a,8);
    p = h;
    for(int i =0;i<3;i++)
        p=p->next;
    del(p);
    PrintfLink(h);
    return 0;

}

3、Write a function string reverse string word By word(string input) that reverse a string word by word.
For instance:
“The house is blue” –> “blue is house The”
“Zed is dead” –>”dead is Zed”
“All-in-one” –> “one-in-All”
在不增加任何辅助数组空间的情况下,完成function
字符串中每个单词(子串)的逆序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 8
char* change(char s[])
{
    char s1;
    int len = 0,i = 0;

    len = strlen(s);
    printf("%d\n", len);

    /*for(i = 0; i < (len/2); i++)
    {
        s1 = s[len-i-1];
        这里不知道为什么无法使用,挂了。 
        s[len-i-1] = s[i];

        s[i] = s[len-i-1];
    }*/

    return s;
}
int main(void)
 { 
    char *s="abcdefgh";
    s = change(s);
    printf("%s\n", s);
    return 0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值