编写程序outch,程序读入一行字符,根据命令行中的参数进行不同的输出。若有以下命令行:out -2
- 提问者网友:听门外雪花风
- 2021-03-16 17:20
- 五星知识达人网友:大漠
- 2021-03-16 18:02
#include
#include
int main(int argc, char *argv[])
{
char buffer1[1024];
char buffer2[100];
int buffer1_len;
int buffer2_len;
int out_len;
printf("Please input string:");
gets(buffer1);
printf("Please input command(usage: out -2): ");
gets(buffer2);
buffer1_len = strlen(buffer1);
buffer2_len = strlen(buffer2);
if ( buffer2_len < 6
|| strncmp(buffer2, "out ", 4) != 0
|| (buffer2[4] != '+' && buffer2[4] != '-')
|| (out_len = atoi(buffer2 + 4)) == 0
|| fabs(out_len) > buffer1_len )
{
printf("error input, will exit.
");
}
if ( fabs(out_len) == buffer1_len )
{
printf("result: %s
", buffer1);
else if ( buffer1_len + out_len < buffer1_len )
{
printf("result: %s
", buffer1 + buffer1_len + out_len);
else
{
buffer1[out_len] = '';
printf("result: %s
", buffer1);
return 0;
}
- 1楼网友:你哪知我潦倒为你
- 2021-03-16 19:34
#include
#include
void getschar(char *a,int *l)
{
printf("Please input a string: ");
gets(a);
*l=strlen(a);
}
void getscommand(char *a,int *n)
{
printf("Please input the command(outch x): ");
gets(a);
if(a[6]=='-') *n=-(a[7]-48);
else *n=a[7]-48;
}
void putschar(char *a,int *n,int *l)
{
int i=0,j=*l-1;
if(abs(*n)>*l&&10>*l)
puts(a);
else if(abs(*n)>*l&&10<*l)
{
for(i=*l-10;i<*l;i++)
printf("%c",a[i]);
printf("
");
}
else if(*n<0)
{
for(i=*l-abs(*n);i<*l;i++)
printf("%c",a[i]);
printf("
");
}
else if(*n>0)
{
for(i=0;i<*n;i++)
printf("%c",a[i]);
printf("
");
}
}
int main()
{
char a[100];
char b[8];
int len,n;
getschar(a,&len);
getscommand(b,&n);
putschar(a,&n,&len);
return 0;
}
- 2楼网友:患得患失的劫
- 2021-03-16 18:46
#include
void outch(char *a,int q)
{
int i;
if(q>0)
{
for(i=0;i }
else if(q<0)
{
for(i=strlen(a)-1;i>strlen(a)-1+q;i--)printf("%c",*(a+i));
}
else {for(i=strlen(a)-1;i>strlen(a)-1-10;i--)printf("%c",*(a+i));}
//当输入数字为0时,反向输出最后10个数
}
main(int argc)
{
char str[100],*s=str;
gets(s);
int n;
printf("outch ");
scanf("%d",&n);//在outch后面输入数字(例:outch -2)
outch(s,n);
}