CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2003
    Posts
    417

    Reversing a string in C

    This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char* StrReverse(char*);
    char* StrReverse1(char*);
    char* StrReverse2(char*);
    void StrReverse3(char*);
    void StrReverse4(char*);
    
    int main(void)
    {
    
    	char str[50];
    	int temp=0;
    
    	printf("Enter a string: ");
    	scanf("%s", str);
    	printf("The reverse of the string is: %s\n", StrReverse(str));
    	printf("The reverse of the string is: %s\n", StrReverse1(str));
    	printf("The reverse of the string is: %s\n", StrReverse2(str));
    
    	StrReverse3(str);
    	printf("The reverse of the string is: %s\n", str);
    	
    	//Get back the original string
    	StrReverse3(str);
    	
    	//Reverse it again
    	printf("The reverse of the string is: ");
    	StrReverse4(str);
    	printf("\n");
    
    	scanf("%d", &temp);
    
    }
    
    
    char* StrReverse(char* str)
    {
    	char *temp, *ptr;
    	int len, i;
    
    	temp=str;
    	for(len=0; *temp !='\0';temp++, len++);
    	
    	ptr=malloc(sizeof(char)*(len+1));
    	
    	for(i=len-1; i>=0; i--) 
    		ptr[len-i-1]=str[i];
    	
    	ptr[len]='\0';
    	return ptr;
    }
    
    char* StrReverse1(char* str)
    {
    	char *temp, *ptr;
    	int len, i;
    
    	temp=str;
    	for(len=0; *temp !='\0';temp++, len++);
    	
    	ptr=malloc(sizeof(char)*(len+1));
    	
    	for(i=len-1; i>=0; i--) 
    		*(ptr+len-i-1)=*(str+i);
    	
    	*(ptr+len)='\0';
    	return ptr;
    }
    
    char* StrReverse2(char* str)
    {
    	int i, j, len;
    	char temp;
    	char *ptr=NULL;
    	i=j=len=temp=0;
    
    	len=strlen(str);
    	ptr=malloc(sizeof(char)*(len+1));
    	ptr=strcpy(ptr,str);
    	for (i=0, j=len-1; i<=j; i++, j--)
    	{
    		temp=ptr[i];
    		ptr[i]=ptr[j];
    		ptr[j]=temp;
    	}
    	return ptr;
    }
    
    void StrReverse3(char* str)
    {
    	int i, j, len;
    	char temp;
    	i=j=len=temp=0;
    
    	len=strlen(str);
    	for (i=0, j=len-1; i<=j; i++, j--)
    	{
    		temp=str[i];
    		str[i]=str[j];
    		str[j]=temp;
    	}
    }
    
    
    
    /*A coooooooooool way of reversing a string by recursion. I found it at this web address
    http://www.geocities.com/cyberkabila/datastructure/datastructuresright_reversestring.htm
    */
    
    void StrReverse4(char *str)
    {
    	if(*str)
    	{
    		StrReverse4(str+1);
    		putchar(*str);
    	}
    }
    Then, I read one guy saying a string could be reversed in one single sweep with the exclusive OR operator. Since then I've been itching to know how. If someone can please share with me, the code to reverse a string with the XOR operator, I'll be grateful.

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    StrReverse1 and StrReverse2 both call malloc so you have to call "free" eventually on the pointer they return. Neither of them modify the string you put in so could take const char *. (const is valid on most C compilers too).

    StrReverse4 is nice and fancy for students and mathematicians, but is not practical. You could output the string backwards using a simple for or while loop.

    You've specified in the subject "C" so I won't bring in C++.

  3. #3
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* rev(char* str)
    {
      int end= strlen(str)-1;
      int start = 0;
    
      while( start<end )
      {
        str[start] ^= str[end];
        str[end] ^=   str[start];
        str[start]^= str[end];
    
        ++start;
        --end;
      }
    
      return str;
    }
    
    int main()
    {
      char str[50]="Reversing a string using XOR";
      puts(rev(str));
    
    }
    **** **** **** **** **/**

  4. #4
    Join Date
    Feb 2003
    Posts
    417
    NMTop40,


    Oh yeah! Thanks a lot for the tips.

    I am confused about the call to free() however. If I were to free the memory, would I do it in the StrReverseX functions or when they've finished, in the main? My guess is in the function where I allocated memory, ie. in the individual StrReverseXs, but I've a question regarding that. When I free the memory and then return the pointer to the calling main function, would the pointer recieved by the main function not be annulled? Just a foolish question. I am sorry, if I sound foolish.

    The other option is I call the free in the main because the heap is shared and the pointer is allocated in the heap. Just guessing.

    And thanks for the constant argument tip.

  5. #5
    Join Date
    Feb 2003
    Posts
    417
    Thanks a world, Guysl. That was tricky. I learnt something out of it.

    What you're saying is for each element A, and it's corresponding trail B, if

    Code:
    A = 0110
    B = 1100
    Then, they can be reversed thus:

    Code:
    Step 1
    
    A = A ^ B
    
    A = 0110
    B = 1100
    
    A = 0110 ^ 1100,
    A = 1010
    
    
    
    Step 2
    
    B = A ^ B
    
    A = 1010
    B = 1100
    
    B = 1010 ^ 1100
    B = 0110
    
    
    
    Step 3: Final
    
    A = A ^ B
    
    A = 1010
    B = 0110
    
    A = 1100
    Thanks a tonne.

    I have another question. This was still on an element-by-element basis. I guess it wouldn't be possible to do it in C in one single sweep, would it? We'll still have to work our way through each element, right?

  6. #6
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    Yes, I can't see a way to avoid it.
    **** **** **** **** **/**

  7. #7
    Join Date
    Feb 2003
    Posts
    417
    Thanks a bunch, Guysl.

  8. #8
    Join Date
    Jun 2012
    Posts
    2

    Reversing a string in C

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

    main()
    {
    char str[100],rev[100];
    int string_len=0,j=0,i;
    printf("Enter a string to reverse\n"); //"Hello Basu"
    gets(str);
    while(*str!='\0')
    string_len++; //10
    for(i=string_len;i>=0;i++)
    {
    rev[j]=str[i];
    j++;
    }



    printf("Reverse of entered string is \n%s\n",rev); //"usaB olleH"

    return 0;
    }

  9. #9
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Reversing a string in C

    This thread is 8 years old.

  10. #10
    Join Date
    Jan 2009
    Posts
    596

    Re: Reversing a string in C

    And the code is buggy as well:

    Code:
    for(i=string_len;i>=0;i++)
    {
    rev[j]=str[i];
    j++;
    }
    @ bmsangati: Did you mean -- ?

  11. #11
    Join Date
    Jun 2012
    Posts
    2

    Re: Reversing a string in C

    okay. thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured