Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to write a program that extracts a certain range of characters from a string

ID: 3671808 • Letter: H

Question

How to write a program that extracts a certain range of characters from a string?

Say the user inputs a string that says "xxxpreemtivelyzzz" and it's my task to extract the string only from the characters 'pre' to 'ly' so the output would be
"preemptively". But if the user inputs a string that has either one of the required prefix or suffix or none of them at all an error message will display.

A function that can extract the output string can have the parameters (char *s1, char *s2); where s1 was the inputted string and s2 is the output.

Another example would be if the user inputs "utsfzprematurelyddsf" the output would be "prematurely". But if the input is "sdkhcrazilyfds", the error message will show because there is no "pre" in the word "crazily". I hope this question makes sense. Thank you!

Explanation / Answer

C code to extract a certain range of characters from a string

#include <stdio.h>
#include <string.h>

int position( char* s1, char* target)         // this function uses strstr The C library function
                                              // char *strstr(const char *haystack, const char *needle) function
                                              // finds the first occurrence of the substring needle in the string haystack.
                                              // The terminating '' characters are not compared
{
  
    char *location;
    int l;
    location = strstr(s1,target);              // location stores the pointer to the target string
    l = (int)location-(int)s1;
   // printf("%d ",l);
    if( location == NULL)                      // if target is not present in s1 return -1
        return -1;
    else
    {
      
       // printf("String '%s' was found at position %d in string '%s'. ",target,l,s1);
    }

    return l;                                     // else return the position of target in s1
}


int main()
{
char s1[1000];                               // initial string
char s2[1000];                                // output string
scanf("%s",s1);                         
char prefix[1000];                          
char suffix[1000];
scanf("%s",prefix);                           // input the range prefix and suffix of s2 to be obtained
scanf("%s",suffix);



int length = strlen(s1);                      // length of s1
int prefix_length = strlen(prefix);           // length of prefix
int suffix_length = strlen(suffix);           // length of suffix

int prefix_in_s1 = position(s1,prefix) ;      // store index of prefix
int suffix_in_s1 = position(s1,suffix);       // store index of suffix


if(prefix_in_s1>= length || prefix_in_s1 < 0) {printf("No substring found "); return 0;}
if(suffix_in_s1>= length || suffix_in_s1 < 0) {printf("No substring found "); return 0;}
// return error if no substring found


int j = suffix_in_s1 + suffix_length;            // finding the end point of output string
int i,k=0;
for ( i = prefix_in_s1; i < j; ++i)
{
    s2[k] =s1[i];
    k++;                                           // adding the elements of s1 to s2 which
}                                               // occurt in range of prefix and suffix

printf("%s ",s2);                             // output s2
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote