c语言怎样截取字符串
1、直接给大家代码吧:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void print(char s[],int n,int m)
{
int k;
int i;
char *p;
k=strlen(s);
p = s;
2、 for(i=n-1;i<n-1+m;i++)//从第n-1位置开始,截取m个字符
putchar(*(p+i));
printf("\n");
}
void main()
{
char *s,str[20];
int m,n;
printf("please input a string:\n");
s = str;
gets(s);
printf("the string is:");
puts(s);
printf("please input n and m\n");
scanf("%d%d",&n,&m);
print(s,n,m);
}
3、题目要调用这个函数:
viod substr(char *source,int start,intlength,char *dest);
// cscs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void substr(char *source,int start,int length,char *dest);
int main()
{
4、 char a[20],b[20],*p;
int n,x,i;
printf("how long:");
scanf("%d",&n);
printf("where:");
scanf("%d",&x);
getchar();
printf("input words:");
gets(a);
p = a;
substr(p,x,n,b);
return 0;
}
5、void substr(char *source,int start,int length,char *dest)
{
int k;
int i;
int j=0;
char *p;
k=strlen(source);
p = source;
for(i=start-1;i<start-1+length;i++)//从第n-1位置开始,截取m个字符
dest[j++] = *(p+i);
dest[j] = '\0';
printf("the new string is:");
puts(dest);
printf("\n");
}