0

I want to extract a substring from an array of characters with a function with signature:

void substring (char str[], int start, int count, char result[]); 

This is my attempt:

void substring (char str[], int start, int count, char result[]) { int i; int j = 0; for(i = start; i <= count; i++) { result[j] = str[i]; j++; printf("the character in function call : %c \n", result[j]); printf("the count of j is : %i \n", j); } result[j] = '\0'; printf("the result function call : %s \n", result); } int main (void) { char pet[] = "character"; char result[40]; substring (pet, 4, 3, result); printf("the substring of character from 4, 3 step ahead is : %s \n", result); return 0; } 

But I get no result at all in my console window. I found another approach on the web with a while loop, but I still think that my code should be working. Why does my code not work?

2
  • for(i=start; i<=count+start; i++) {} Commented Jul 27, 2017 at 13:58
  • Better: for (j = 0; j < count; j++) { result[j] = str[j + start]; } result[j] = '\0';. Note that the code does not check that the string is big enough — it assumes it is OK to access the elements str[start] through str[start+count-1] without checking that there isn't an earlier null byte, etc. That may be OK — it's a design decision that should be conscious rather than unconscious, though. Commented Jul 27, 2017 at 14:56

3 Answers 3

1

The problem is in this line:

for(i = start; i <= count; i++) 

The first time it enters in this for loop, i is 4, which is lower than count whose value is 3. So it exits the loop and it does not print anything.

You have to change it for:

for(i = start; i < start + count; i++) 
Sign up to request clarification or add additional context in comments.

Comments

1

The condition of your for loop is wrong. It should be i < start + count, so that your for loop have to be:

for (i = start; i < start + count; i++) { ... } 

to count in your case e.g. 3, 4, 5, 6 until the end (6 will not be executed) is reached, also note the < is used and not the <=.
Output will be: "act" in your case.

You can substitute your for loop where you copy every single character by strncpy which is described at this manpage (synoptics: char *strncpy(char *dest, const char *src, size_t n);).

It would be like this:

strncpy(result, str + start, count); result[start + count] = '\0'; 

Further you should think of making your function robust against buffer overflows of the input (str) and output (result) string by using strlen on the input buffer and passing the buffer size of the output buffer to the function and checking these lengths against start and count. Another possibility would be to allocate the output buffer dynamically with the right size but nevertheless the input buffer should be checked.

2 Comments

must be i < start + count
Hi @Benedictus if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
0
void substring (char str[], int start, int count, char result[]) { int i; int j = 0; for(i = start; i <= count + start; i++) { // your problem are i <= count result[j] = str[i]; j++; printf("the character in function call : %c \n", result[j]); printf("the count of j is : %i \n", j); } result[j] = '\0'; printf("the result function call : %s \n", result); } 

OR

Fast substring without looping

#include <stdio.h> #include <string.h> void substring (char *str, int start, int count, char *result) { if((start + count) > strlen(str)){ // ensuring the data to be taken // does not exceed the length of the data. result = '\0'; } strncpy(result, str + start, count); // The copy of the character from str to result // starting from the start along count characters. result[start+count] = '\0'; // null terminator } int main(){ char ret[128]; char *src = "character"; // similiar with src[] = memset(ret, '\0', 128); // clear retbuffer substring(src, 4, 3, ret); // get src from index 4 along 3 characters. printf ("result= %s\n", ret); // result= act return 0; } 

3 Comments

Just a code dump with no explanation is a bad answer.
This version would not work if you don't initialize it before as you did with memset. The trailing '\0' should be appended in the substring function.
Then your memset in the main function will be obsolete now. Further your version is not overflow save. You check the length but if it exceeds the strncpy will still be called. As I write in my answer providing the buffer sizes would be a possibility.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.