c语言怎样截取字符串
今天小编给大家带来的是c语言怎样截取字符串,希望能帮助到大家!
操作方法
- 01
直接给大家代码吧: #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;
- 02
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); }
- 03
题目要调用这个函数: 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() {
- 04
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; }
- 05
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"); }