linux 设计一个程序cuts,他由标准输入读取数据,获取由第一个参数N和第二个参数M所限定范围的数据

n和m都是整数,即从输入的字符串中抽取第n个字符至第m个字符之间的所有字符(包括这两个字符)。例如:
﹩cuts 11 14
this is a test of cuts program (输入)
test (显示结果)

第1个回答  2012-06-12
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char *argv[])
{
char *pstr;
char buf[255];
int begin,end;
int i;
if(argc !=3)
{
printf("error of inputs\n");
exit(1);
}
pstr = (char *)malloc(sizeof(char)*30);
sscanf(argv[1],"%d",&begin);
sscanf(argv[2],"%d",&end);
printf("begin = %d,end = %d\n",begin,end);

printf("inputs the string:\n");
fflush(stdout);
gets(buf);
printf("the input is \n");
printf(buf);
printf("\n");
for(i=begin;i<=end;i++)
{
*(pstr+i-begin)=*(buf+i-1);
}
*(pstr+i-begin)='\0';
printf("the output is :\n%s\n",pstr);

}
相似回答