C练习-(五)

本文介绍了三种实用的编程技巧:判断字符串是否为回文、删除单向链表中的指定节点以及反转字符串中的单词顺序。通过具体的代码示例,帮助读者理解和掌握这些常见算法问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值