Tuesday, December 13, 2011

String Reverse / Print Reverse

Reverse String

1.Whole string-- iterative,recursive & inplace
http://www.geeksforgeeks.org/write-a-program-to-reverse-an-array-or-string/
http://www.geeksforgeeks.org/reverse-a-string-using-recursion/
http://stackoverflow.com/questions/7569335/reverse-a-string-in-java
2.The Words in a string-- the world will go on forever ---"Forever on go will world the
http://www.programcreek.com/2014/02/leetcode-reverse-words-in-a-string-java/
http://www.programcreek.com/2014/05/leetcode-reverse-words-in-a-string-ii-java/
http://www.geeksforgeeks.org/reverse-words-in-a-given-string/
3.Words one by one-- the world will go on forever ---- reveroF no og lliw dlrow eht
4  Swap two words in a given sentence
ex: my name is Khan in school.
swap name and in in the above sentence.
o/p : my in is name school.
5.Given only putchar (no sprintf, itoa, etc.), Print a string in reverse order.

Perfect reversible string
You are given a string ‘str’, the task is to check that reverses of all possible substrings of ‘str’ are present in ‘str’ or not.
http://www.geeksforgeeks.org/perfect-reversible-string/

Reverse String:
Recursive:

int reverse(char *str,int pos)
{
if(pos < (strlen(str)/2))
{
char ch;
ch = str[pos];
str[pos]=str[strlen(str)-pos-1];
str[strlen(str)-pos-1]=ch;

reverse(str,pos+1);
}
}

int main() {
static char str[]="STRING TO REVERSE";
printf("\nOriginal string : [%s]", str);

reverse(str,0);

printf("\nReversed string : [%s]", str);
getchar();
return(0);
}
In-Place: Iterative
void rev(char *l,char *r)
{
char t;
while(l < r)
{
t = *l;
*l++ = *r;
*r-- = t;
}}

int main(int argc, char *argv[])
{
char buf[] = "the world will go on forever";
char *end;

for(end=buf; *end; end++);
rev(buf,end-1);

printf("%s\n",buf);
getchar();

return(0);
}

2. Reverse Words:
void rev(char *l,char *r)
{
char t;
while(l < r)
{
t = *l;
*l++ = *r;
*r-- = t;
}
}

int main(int argc, char *argv[])
{
char buf[] = "the world will go on forever";
char *end, *x, *y;

// Reverse the whole sentence first..
for(end=buf; *end; end++);
rev(buf,end-1);

// Now swap each word within sentence...
x = buf-1;
y = buf;

while(x++ < end)
{
if(*x == '\0' || *x == ' ')
{
rev(y,x-1);
y = x+1;
}
}

// Now print the final string....
printf("%s\n",buf);
getchar();
return(0);
}

OR USING XOR

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

#define SWAP(a,b) (a)^=(b)^=(a)^=(b)


int main()
{
    printf("Enter a string\n");
    char str[256];
    gets(str);
    
    int len = strlen(str);
    
    // Reverse the string with iterators starting from back and front
    int i= 0;
    int j = len-1;
    // We have to continue while the first pointer is less than the second
    while(i < j)
    { 
        SWAP(str[i],str[j]);
        i++;
        j--;
    }
        
    // Now reverse the words with in the string
    i=0;
    j=0;
    //while the first iterator does not reach the end of the string
    while(str[i]!='\0')
    {
        // while there are white spaces, skip over them
        while(str[j]==32)
        {
            i++;
            j++;
        }
        // While there are characters not separated by a space move j forward
        // when j is moving over the last word then we must take care of the
        // end terminating character as well
        while(str[j]!=32 && str[j]!='\0')
            j++;
        
        // store this position of j in a temp variable
        int temp=j;
        j--;
        
        // Use swapping to reverse the substring formed between i and j-1
        while(i < j)
        {
            SWAP(str[i],str[j]);
            i++;
            j--;
        }
        // set the value of i and j to temp
        i=j=temp;
    }
    printf("The string reversed word by word is %s",str);
    getchar();
    return 0;
}

3. Words one by one:
void ReverseInPlace(char * pstart, char * pend)
{
    char tmp;

    while (pend > pstart)
    {
        tmp = *pstart;
        *pstart++ = *pend;
        *pend-- = tmp;
    }
}
void ReverseWordsInPlace(char *pSentence) {
    char * pstart;
    char * pend; 
    pstart = pSentence;

    while (*pstart != '\0')
    {
        // skip any (multiple) starting spaces
        while (*pstart == ' ')
        {
            pstart++;
        }

        pend = pstart;

        // find end of word (terminated by a space or end of string)
        while (*pend != ' ' && *pend != '\0')
        {
            pend++;
        }

        // check if anything left to do
        if (pstart >= pend - 1)
            return;

        ReverseInPlace(pstart, pend - 1);

        pstart = pend;
    }
}

4. Swap Words:
Solution : 
Reverse the entire string  from starting word to ending word : Here starting word is name and ending word is in.So reverse the string from name to in using inplace string reversal algo.It requires O(n) traversal.
Step 1: 
Str = my name is Khan in school.
Str = my ni nahK si eman school
Step 2:
Now reverse the first word of the modifies string:
Str = my ni nahK si eman school
Str = my in nahk si eman school
Step 3 :
Now reverse the string after first word to second modified word.
Str = my in nahk si eman school
Str = my in is Khan eman school(after reversal)
Step 4 :
Now reverse the second word
Str = my in is Khan school
Str= my in is Khan name school

Original sentence         : my name is Khan in school.
After final modification : my in is Khan name school
Got the desired output.in and name are swapped in the given sentence.

5. Reverse String with putchar:
void printReverse(const char *str) {
  if (!*str)
    return;
  printReverse(str + 1);
  putchar(*str);
}

No comments:

Post a Comment