C语言内不存在String类型,我们如何用基础的数据结构来实现字符串截取呢?
#include <stdio.h>
int main(){
int i, j, length;
char a[] ="hello world";
int from, num;
scanf("%d",&from); //截取开始的字符下标
scanf("%d",&num); //共截取的字符数
char b[num];
for (length = 0; a[length]!= '\0'; length++){}
if (from>=0)
if (from+num <= length ){
i = from; j=0;
while ( j<num){
b[j] = a[i];
printf("%c",b[j]);
j++; i++;}
}return 0;
}
(待续--使用指针实现)