day10 用分文件编译和makefile实现以单词为单位进行字符串倒置,不用数组而是用动态申请的内存。

1

函数代码:

#include"head.h"
char* create(int size){
	char*p=(char*)malloc(size);
	return p;
}
void my_invert(char* str){
	int len=strlen(str);
	int i=0,j=0,k=0;
	while(i<len/2){//整体导致
		invert(str+i,str+len-i-1);
		i++;
	}
	puts(str);
	for(i=0;i<len;i++){
		while(*(str+i)==' '){//找到单词的开头
			i++;
		}
		 j=i;
		while(*(str+j)!=' '&&*(str+j)!=0){//找到单词末尾
			j++;
		}
		k=j;
		j--;
		while(i<j){
			invert(str+i,str+j);//单词内倒置
			i++;
			j--;
		}
	i=k;//定位到单词后的空格
	}
}
char* my_free(char* str){
	if(str==NULL){
		return NULL;
	}
	free(str);
	return NULL;
}
void invert(char*p1,char*p2){
	int temp=0;
	temp=*p1;
	*p1=*p2;
	*p2=temp;
}

头文件代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
extern char* create(int );
extern void my_invert(char*);
extern char* my_free(char*);
extern void invert(char*,char*);

主函数代码

#include"head.h"
int main(int argc, const char *argv[])
{
	char*p=create(32);
	printf("请输入字符串");
	gets(p);
	puts(p);
	my_invert(p);
	printf("%s\n",p);
	p=my_free(p);
	return 0;
}

 makefile代码

CC=gcc
CFLAGs=-c -Wall -g
EXE=myprogram
OBJS=$(wildcard *.c)
OBJs=$(patsubst %.c,%.o,$(OBJS))
all:$(EXE)
$(EXE):$(OBJS)
	$(CC)  $^ -o $@ 
clean:
	rm *.o $(EXE)
.PHONY :clean

结果:

ubuntu@ubuntu:invert_str$ make
gcc  main.c fun.c -o myprogram 
main.c: In function ‘main’:
main.c:6:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
  gets(p);
  ^~~~
  fgets
/tmp/ccUwP2Kz.o:在函数‘main’中:
main.c:(.text+0x3b): 警告: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:invert_str$ ./myprogram 
请输入字符串this is a book
this is a book
koob a si siht
book a is this

最近更新

  1. TCP协议是安全的吗?

    2024-01-26 21:18:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-26 21:18:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-26 21:18:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-26 21:18:01       18 阅读

热门阅读

  1. 微信小程序打卡定位实现方案

    2024-01-26 21:18:01       36 阅读
  2. 《More Effective C++》《效率——16、谨记80-20法则》

    2024-01-26 21:18:01       33 阅读
  3. R语言【taxlist】——clean_strings():清理字符串

    2024-01-26 21:18:01       33 阅读
  4. 深度学习简介与应用

    2024-01-26 21:18:01       34 阅读
  5. 铭飞获取幻灯片栏目下的图片

    2024-01-26 21:18:01       30 阅读
  6. React进阶 - 13(说一说 React 中的虚拟 DOM)

    2024-01-26 21:18:01       44 阅读
  7. Hive之set参数大全-14

    2024-01-26 21:18:01       28 阅读
  8. SpringBoot实现自定义异常+全局异常统一处理

    2024-01-26 21:18:01       35 阅读
  9. 深入理解高阶函数与函数柯里化在React中的应用

    2024-01-26 21:18:01       39 阅读
  10. MySQL之约束

    2024-01-26 21:18:01       31 阅读