Wednesday, 16 May 2012

Write a C program to reverse a substring of a string without using any library function.

Underlying Concept: String manipulation

#include<stdio.h>
#include<stdlib.h>
int sub_reverse(char *s,char *r,char *t)
{
    int i=0;
    while(s[i]!=0)
    {
        if(s[i]==r[0])
            break;
        i++;
    }
    char temp;
    while(s<t)
    {
        temp=*s;
        *s=*t;
        *t=temp;
        s++;
        t--;
    }
}


int main()
{
    char *string,*sub,*end;
    unsigned u,pos1,pos2;
    printf("\n enter size of string:");
    scanf("%u",&u);
    string=(char *)malloc(u*sizeof(char));
    printf("\n enter the string:");
    scanf("%s",string);
    printf("\n What is the position of first character of substring:");
    scanf("%u",&pos1);
    printf("\n What is the position of last character of substring:");
    scanf("%u",&pos2);
    sub=string+pos1;
    end=string+pos2;
    sub_reverse(string,sub,end);
    printf("\nNew string is:%s",string);
    return 0;
}

0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top