1.现有文件test.c\test1.c\main.c,请编写Makefile
CC=gcc
EXE=test
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个
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[20];
gets(s);
int i = 0;
int count = 1;
while (s[i] != '\0')
{
if (s[i] == ' ')
{
count++;
}
i++;
}
printf("%d个\n", count);
return 0;
}

3.在终端输入一个文件名,判断文件的类型
#!/bin/bash
read -p '请输入文件:' f
if [ -d $f ]
then
echo dir
elif [ -p $f ]
then
echo pipe
elif [ -L $f ]
then
echo link
elif [ -S $f ]
then
echo socket
elif [ -f $f ]
then
echo regular
fi

4.字符串倒置:(注意:是倒置,而不是直接倒置输出)
原字符串为: char *str =“I am Chinese"
倒置后为:“Chinese am I”
附加要求:删除原本字符串中多余的空格 附加要求:删除原本字符串中多余的空格
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s[] = "i am chinese";
int len = 0;
for (; s[len] != '\0'; len++);
int i = 0, j = len - 1;
while (i <= j)
{
char t = s[i];
s[i] = s[j];
s[j] = t;
i++;
j--;
}
i = 0;
j = 0;
while (i < len)
{
while (s[j] != ' ' && s[j] != '\0')
{
j++;
}
int k = j - 1;
while (i <= k)
{
char t = s[i];
s[i] = s[k];
s[k] = t;
i++;
k--;
}
while (s[j] == ' ')
{
j++;
}
i = j;
}
puts(s);
return 0;
}

本文介绍了如何编写Makefile来编译C源文件,包括C语言编程实现单词计数、在终端判断文件类型以及字符串反转去除多余空格的方法。
1万+

被折叠的 条评论
为什么被折叠?



