**Write a function called subString() to extract a portion of a character string. The function should be called as follows
subString (source, start, count, result);
Where source is the character string from which you are extracting the substring, start is an index number into source indicating the first character of the substring, count is the number of characters to be extracted from the source string, and result is an array of characters that is to contain the extracted substring.**
Here is what I have so far. I'm getting a weird result when I run the program
#include <stdio.h>
char substring (char source[], int start, int count, char result[])
{
int i;
source[start] = result [0]; // set the initial value of the result array to the value of the source array at the point of 'start'
for (i = 1; i <= count; i++, start++) { // keep adding to start until 'result' has obtained all the values it was supposed to get from the source array
source[start] = result[i];
}
result [count+1] = '\0'; // adds a null terminator to the end of the resultant string
return *result;
}
int main (void)
{
char source [] = "Any help would be appreciated. I dont know where I've gone wrong"; // define a generic string for the user to extract a string from
int start = 0, count = 0;
char result [255];
// allows for multiple different calls to be made without having to adjust the code every time
printf("%s",source);
printf("\n\nAt what point in the above statement should extracting begin? "); // assigns a starting point
scanf("%d", &start);
printf("\n\nHow many characters do you want to extract? "); // sets a number of characters to extract
scanf("%d", &count);
substring (source, start, count, result); // call the substring function
printf("\n\nThe resultant string, after extraction is: %s\n\n", substring); // print the result of the subString function
return 0;
}
Aucun commentaire:
Enregistrer un commentaire