作业2024/2/8

1.现有文件test.cltest1.clmain.c,请编写Makefile

CC=gcc
EXE=a.out
OBJS=$(patsubst %.c,%.o,$(wildcard *.c))
CFLAGS=-c -o
 
all:$(EXE)
 
$(EXE):$(OBJS)
	$(CC) $^ -o $@
 
%.o:%.c
	$(CC) $(CFLAGS) $@ $^
 
.PHONY:clean
 
clean:
	@rm $(OBJS) $(EXE)

2.C编程实现: 输入一个字符串,请计算单词的个数
eg: "this is a boy"
输出单词个数:4个

int main(int argc, const char *argv[])
{
	char str[50]="";
	printf("please enter str:");
	gets(str);
	int num=0;
	char c;
	for(int i=0;str[i]!='\0';i++)
	{
		if(str[i]==' '){
			num++;
		}
	}
	printf("%d\n",num+1);
	return 0;
}

3.在终端输入一个文件名,判断文件的类型


#!/bin/bash
 
read -p "please enter file:" file 
 
if [  -b ./$file ]     
then
    echo block
elif [ -c ./$file ]   
then
    echo character
elif [ -d ./$file ] 
then
    echo directory
elif [ -f ./$file ]  
then
    echo file
elif [ -L ./$file ]  
then
    echo line
elif [ -S ./$file ]    
then
    echo socket
elif [ -p ./$file ]
then
    echo pipe
else
    echo unexist               

4.C编程实现
字符串倒置:(注意:是倒置,而不是直接倒置输出)
原字符串为:char *str =“Iam Chinese
倒置后为:“Chinese am I”

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char str[30]="";
	printf("please enter str:");
	gets(str);
	
	int i=0,j=0;
	while(str[i]!='\0')
	{
		if(str[i]==' '){
			str[j]=str[i];
		}
		j++;
		i++;
	}
	str[j]='\0';
	puts(str);
	//整体逆置
	i=0,j=strlen(str)-1;
	while(i<j)
	{
		char t=str[i];
		str[i]=str[j];
		str[j]=t;
		i++;
		j--;
	}
	
	i=j=0;
	while(str[i]!='\0')
	{
		while(str[j]!=' ' && str[j]!='\0')
		{
			j++;
		}
		int k=j-1;
		while(i<k)
		{
			char t=str[i];
			str[i]=str[k];
			str[k]=t;
			i++;
			k--;
		}
		while(str[j]==' ')
		{
			j++;
		}
		i=j;
	}
	puts(str);
 
	return 0;
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值