Write a function
subject
Please write a function that extracts a string from a string. The prototype of the function is as follows: int substr(char dst[],char src[],int start,int len); The task of the function is to copy a maximum of len non NUL characters to the dst array by offsetting the start position of the src array backward by start characters. After copying, the dst array must end in NUL bytes. The return value of the function is the length of the string stored in the dst array.
If the position specified by start crosses the tail of the src array, or the value of start or len is negative, an empty string is copied to the dst array.
My thoughts
When I first saw the problem, I thought it was not very simple? (my friend thought so, and he wrote it too)
And then..... Infinite cycle
Of course, it's not just him. It took me a long time to debug it, but my point is different from him. I'm judging the length of the array, which is wrong. (described below)
The idea of writing programs is very important. Here are three obvious points:
- Start by offsetting the start position of src array backward by start characters, and copy len to dst array at most.
- The return value of the function is the length of the string that stores the dst array.
- If the position specified by start crosses the tail of the src array, or the value of start or len is negative, an empty string is copied to the dst array. (I feel like I've read the topic again)
The point where I made a mistake is to copy the content, because the first point is that my judgment statement is based on the length of the array, and I use the sizeof() function. After debugging, I found that the value of the array returned by it has always been 1. Later, baidu found a blog,
Find that you have used it incorrectly, and then use the strlen() function to solve the problem. Attach the link to this blog: https://blog.csdn.net/m0_37592397/article/details/79701992
The topic is to write functions, but we can't just write functions, but we also need to test, so we still need to write the whole program, because the main task is to write functions, so I directly define several functions, offsets and maximum values, so I don't need user input. At the beginning, I thought about user input, but because there are too many errors at the beginning, I just completed the main task.
First, define the standard library and constants to be used
#include<stdio.h> #include<stdlib.h> #include<string.h> #define x 10
Here, < stdlib. H > is used to return success and failure, < string. H > uses the function of string length to define a constant x, so as to define the array
Main function
int main() { char dst[x]; char src[x]="abcdefjhi"; int start=3,len=10; printf("character string:%s,deviation%d Characters,Maximum replication%d Characters\n",src,start,len); substr(dst,src,start,len); return EXIT_SUCCESS; }
It's very simple here. I won't say more
Mainline task: writing functions
int substr(char dst[x],char src[x],int start,int len) { int i=0; //As subscript if(start>len||start<0||len<0) //Judge that start and len are negative values { dst[x]=NULL; return strlen(dst); } while(i<len) { if(strlen(src)-start<=len) //When the conditions are met, copy { dst[i]=src[start+i]; } else { return EXIT_FAILURE; } if(strlen(src)-start==i+1) //When the src is copied from the offset, the loop ends { printf("Substring:%s\n",dst); return strlen(dst); } i++; } }
The first judgment is to complete the third point. If the third point is met, it is directly copied to a null value, and then it is a copy cycle under normal circumstances. The code is very simple, so I won't talk about it.
Full code:
#include<stdlib.h> #include<string.h> #define x 10 int substr(char dst[x],char src[x],int start,int len) { int i=0; //As subscript if(start>len||start<0||len<0) //Judge that start and len are negative values { dst[x]=NULL; return strlen(dst); } while(i<len) { if(strlen(src)-start<=len) //When the conditions are met, copy { dst[i]=src[start+i]; } else { return EXIT_FAILURE; } if(strlen(src)-start==i+1) { printf("Substring:%s\n",dst); return strlen(dst); } i++; } } int main() { char dst[x]; char src[x]="abcdefjhi"; int start=3,len=10; printf("character string:%s,deviation%d Characters,Maximum replication%d Characters\n",src,start,len); substr(dst,src,start,len); return EXIT_SUCCESS; }
Operation effect:
Finally, although it took a long time, there was still a harvest.
There may be many errors in this program. I didn't debug it, but I won't debug it.