1.
写一函数int fun(char *p)判断一字符串是否为回文,是返回1,不是返回0,出错返回-1.(例如:字符串”123454321”就是回文字符串)
int fun(char *p)
{
int len = strlen(p);
int over_half = len%2 + len/2; //后半部第一个标号
int before_half = len/2 - 1; //前半部第一个标号
while(p[over_half] != 0 && (p[before_half--] == p[over_half++]));
return p[over_half] ? 0: 1;
}
2、假设现有一个单向的链表,但是只知道只有一个指向该节点的指针p,并且假设这个节点不是尾节点,试编程实现删除此节点。
节点结构:struct node
{
int data;
struct node *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>
#include <string.h>
#include <stdlib.h>
void left_shift(char *p, unsigned int offset,unsigned len)
{
unsigned int i;
char temp ;
p[len] = ' '; //移位时多出个空字符,需要个temp
while(offset-- > 0){ //一会给offset的实参都是>0的
i = 0;
temp = p[0];
while ( i < len)
p[i++] = p[i+1];
p[len] = temp;
}
}
void flip(char *p)
{
unsigned int len = strlen(p);
unsigned int temp_len = len;
unsigned int offset = 0;
while (1){
while(p[offset++] != ' ');
if(len == offset - 1)
break;
left_shift(p, offset , len);
len -= offset ;
offset = 0;
}
p[temp_len] = 0;
}
int main(void)
{
char a[] = "the hous blue you ls";
flip(a);
printf("%s",a);
getchar();
return 0;
}