1.
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.
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
fi
4.
#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;
}