1、写一函数int fun(char *p)判断一字符串是否为回文,是返回1,不是返回0,出错返回-1.(例如:字符串”123454321”就是回文字符串)
int fun(char *p)
{
int i = 0;
char *q = NULL;
if(*p == 0)
return -1;
while(*(p+i))
i++;
q = p+i-1;
while(p < q)
if(*(p++) != *(q--))
return 0;
return 1;
}
2、假设现有一个单向的链表,但是只知道只有一个指向该节点的指针p,并且假设这个节点不是尾节点,试编程实现删除此节点。
节点结构:struct node
{
int data;
struct node *p_next;
};
typedef struct node
{
int data;
struct node *p_next;
}node;
void deleteNode(node *p)
{
if(p -> p_next != NULL)
{
p -> data = p -> p_next -> data;
p -> p_next = p -> p_next -> p_next;
free(p -> 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
字符串中每个单词(子串)的逆序
void strReverse(char *str)
{
//先将整个字符串倒序
char *p_end = str;
char *p_start = str;
char tmp = 0;
int i = 0;
do{
p_end++;
}while(*p_end);
for(i = 0; str + i < p_end - i; i++)
{
tmp = *(str + i);
*(str + i) = *(p_end - i);
*(p_end - i) = tmp;
}
//将每个单词逆序;
p_end = str;
while(*p_end)
{
p_start = p_end;
do{
p_end++;
}while(*p_end >= 'A' && *p_end <= 'Z' || *p_end >= 'a' && *p_end <= 'z');
for(i = 0; p_start + i < p_end - i; i++)
{
tmp = *(p_start + i);
*(p_start + i) = *(p_end - i);
*(p_end - i) = tmp;
}
p_end+=2;
}
}