2017.11.18 C

来源:互联网 发布:电脑软件怎么卸载 编辑:程序博客网 时间:2024/05/19 00:39

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

#include<stdio.h>#include<string.h>int fun (char*p){     int len = strlen(p) - 1;  //获取字符串长度    char *q = p+len;  //将指针指向字符串末尾    if (!p)             //判断指针是否为空        return -1;      while(p<q)      {          if ((*p++) != (*q--))  //回文判断            return 0;      }      return 1;}    main(){    char a[]="123321";    printf("%d\n",fun(a));}

2、假设现有一个单向的链表,但是只知道只有一个指向该节点的指针p,并且假设这个节点不是尾节点,试编程实现删除此节点。

节点结构:struct node
{
int data;
struct node *p_next;
};

p->data=p->next->data;p->next=p->next->next;free(p->next);

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>void fun(char *str){    char *head = str;//记录头指针    while (*str++ != 0);//指针移动到结束符后一位    str -= 2;//指向最后一个有效字符    while (1)    {        while (*str >= 'A'&&*str <= 'Z' || *str >= 'a'&&*str <= 'z')            str--;//指向最后的间隔符号        printf("%s",++str);        if (str == head)            return ;        printf("%c", *(--str));//打印间隔符号        *(str) = 0;//设置间隔符号为结束符        str --;    }}void main(){    char s[] = "The house-is-blue";    fun(s);}
原创粉丝点击