C语言每周三道题11.18

来源:互联网 发布:织梦if标签 编辑:程序博客网 时间:2024/06/11 19:14

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


答案:

#include<stdio.h>

#include<string.h>

int fun(char* p)

{

if(*p!=0)

{

int len=strlen(p);

if(len==1)

return 1;

    int i=0;

while(i<len/2)

    {

     if(p[i]==p[len-i-1])

        i++;

else

{

return 0;

}

}

return 1;

}

return -1;

}

 

int main(void)

{

char * a="121";

printf("%d\n",strlen(a));

int ret= fun(a);

printf("%d\n",ret);

return 0;

}


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

节点结构:struct node

{

int data;

struct node *p_next;

};


答案:

# include<stdio.h>

 struct node

 {

 int date;

 struct node * p_next;

 };

 int main()

 {

 struct node test1;

 struct node test2;

 struct node test3;

 struct node * p;

 test1.p_next=&test2;

 test2.p_next=&test3;

 p=&test2;

 (p-1)->p_next=p->p_next;

 p=NULL;

 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<string.h>

void word(char *a)

{

int i,j;

int flag=0;

char temp;

int length=strlen(a);

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

{

temp=a[i];

a[i]=a[length-1-i];

a[length-1-i]=temp;

}

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

{

while(!((a[i]<90&&a[i]>65)||(a[i]<122&&a[i]>97)))

{

i++;

}

flag=i;

while((a[i]<90&&a[i]>65)||(a[i]<122&&a[i]>97))

{

i++;

}

int p=(i-flag)/2;

for(j=0;j<p;j++)

{

temp=a[flag+j];

    a[flag+j]=a[i-1-j];

a[i-1-j]=temp;

}

}

}

 

int main(void)

{

char a[]="The color is bule";

 word(a);

 printf("%s\n",a);

return 0;

}

原创粉丝点击