Showing posts with label reverse till a character. Show all posts
Showing posts with label reverse till a character. Show all posts

Thursday, 7 February 2019

Reverse String Till Underscore

String S is passed as the input to the program. S may or may not have a single underscore embedded in it. The program must reverse the String S till the first underscore and print it as the output.

Input Format:
The first line contains S.

Output Format:
The first line contains the string S modified based on the given conditions.

Boundary Conditions:
Length of S is from 3 to 100.

Example Input/Output 1:
Input:
abcd_pqrs
Output:
dcba_pqrs

Example Input/Output 2:
Input:
_kilo
Output:
_kilo

Example Input/Output 3:
Input:
nounderscore
Output:
erocsrednuon

Program

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

int main()

{
  char s[100],rev[100];
  int i=0,j=0,m,fnd=0;
  scanf("%[^\t\n]s",s);
  
  while(s[i] != '\0' && s[i] != '\n')
  {
     
     if (s[i]!='_')
       rev[j++]=s[i];
     else
        {
            for(j=j-1;j>=0;j--)
            printf("%c",rev[j]);
            fnd=1;
        }
        if (fnd==1)
            printf("%c",s[i]);
    ++i;
  }
  if (fnd==0)
  {
    for(j=strlen(s)-1;j>=0;j--)
    printf("%c",s[j]);
  }



OUTPUT
fsdf 35345 sfsdfsdf_good
fdsfdsfs 54353 fdsf_good