C Programming – HackerRank Solution | EXTRACT THE STRING |

(HackerRank) Write a Modular C Programming code to solve EXTRACT THE STRING, Write a modular C program that extracts part of a given string from a specified position.

Write a modular C program that extracts part of a given string from a specified position. For example, if the string is “working with string is fun”, then if from position 4, 4 characters are to be extracted then the program should return the string as “king”.

If the number of characters to be extracted is 0 then the program should extract the entire string from the specified position.

MODULAR TASK:

Write a minimum of 3 User Defined Functions (UDFs). You are free to decide return type and arguments to be passed to each UDF.

Input Format

First-line indicates the input string.

Second-line indicates start position “s” and the number of characters to be extracted “e”.

Constraints

Input string should have minimum 5 characters.

0 <= s <= maximum length of string.

e <= (maximum length of string – s).

Output Format

Extracted string starting from “s” upto “e” characters.

Sample Input 0

working with string is fun
4 4

Sample Output 0

king

Sample Input 1

strings in C programming.
0 0

Sample Output 1

strings in C programming.

Sample Input 2

working with string is fun
26 5

Sample Output 2

Cannot extract string.

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

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

int main() {
    char string[50];
    int i=0,s,e,l;
    gets(string);
    scanf("%d%d",&s,&e);
    l=strlen(string);
    if(s>=0 && s<=l && e<=l-s && l>5)
    {
    if(e==0)
    {
        i=s-1;
        if(i==-1)
            i=0;
    while(string [i]!='\0')
    {
        printf("%c",string[i]);
        i++;
    }
    }
    else if(s>=l || s+e>=l)
    {
        printf("Cannot extract string.");
    }
    else{
        i=s-1;
    while(i<s+e && string [i]!='\0')
    {
        printf("%c",string[i]);
        i++;
    }
    }
    }
    else{
        printf("Cannot extract string.");
    }
    
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

working with string is fun
4 4
Your Output (stdout)

king 
Expected Output

king

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!