Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Reverse String Program In C


    how can i reverse a string in c..? Please help me..
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,383
    Rep Power
    1871
    You know, 6 posts in quick succession, each with NO EFFORT WHATSOEVER is a sure fire way of getting onto everyone's ignore list.

    Given that your first post was "i have plan for develop a website for programming tutorials", it seems to me that you're just fishing for material.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    very easy..

    most programming books usually include example programs of such kind...

    And I'm not sure, but maybe one of the standard libraries already does contain such a function...(I don't know all the string functions by heart, try googling)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Originally Posted by salem
    You know, 6 posts in quick succession, each with NO EFFORT WHATSOEVER is a sure fire way of getting onto everyone's ignore list.

    Given that your first post was "i have plan for develop a website for programming tutorials", it seems to me that you're just fishing for material.
    and somehow managing to fail even at that lol...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by avidwan
    how can i reverse a string in c..? Please help me..
    #include <stdio.h>
    void reverse_string(char[] arr);
    void main()
    {
    char arr[30];
    int i=0;
    printf("Enter String of 6 character\t");
    for(i=0;i<6;i++)
    {
    scanf("%c",&arr[i]);
    }
    print("Reverse of string is\n");
    reverse_string(arr);
    getch();
    }
    void reverse_string(char[] arr)
    {
    char arr2[30];
    int i=0, j=0;
    for(i=0,j=5;i<6;j--,i++)
    {
    arr2[i] = arr[j];
    }
    for(i=0;i<6;i++)
    {
    printf("%c",arr2[i]);
    }
    }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    You can simple use string reverse function strrev
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main()
    {
    char str1[10]="Hello";
    
    strrev(str1);
    
    puts(str1);
    
    getch();
    return 0;
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Well, I offer the following as an example for your programming website

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* reverse_string(char* str)
    {
    	for(int beg = 0,  end = strlen(str) - 1; beg < end; ++beg, --end)
    	{
    		str[beg] ^= str[end];
    		str[end] ^= str[beg];
    		str[beg] ^= str[end];
    	}
    	return str;
    }
    
    int main(void)
    {
    	char buffer[]="Hello World";
    	printf("%s\n", reverse_string(buffer));
    	return 0;
    }

    Comments on this post

    • codergeek42 agrees : A clever use of XOR-swap algorithm.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Originally Posted by Sachin Bali
    #include <stdio.h>
    void reverse_string(char[] arr);
    void main()
    {
    char arr[30];
    int i=0;
    printf("Enter String of 6 character\t");
    for(i=0;i<6;i++)
    {
    scan("%c",&arr[i]);
    }
    print("Reverse of string is\n");
    reverse_string(arr);
    getch();
    }
    void reverse_string(char arr)
    {
    char arr2[30];
    int i=0, j=0;
    for(i=0,j=5;i<6;j--,i++)
    {
    arr2[i] = arr[j];
    }
    for(i=0;i<6;i++)
    {
    print("%c",arr2[i]);
    }
    }
    are you making some mistakes or is that some other version of C where "scan" works instead of "scanf"...?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by zedeneye1
    are you making some mistakes or is that some other version of C where "scan" works instead of "scanf"...?
    By mistake. It should be "scanf" instead of "scan".
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Originally Posted by Sachin Bali
    By mistake. It should be "scanf" instead of "scan".
    and printf and several many other errors...
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by zedeneye1
    and printf and several many other errors...
    I already updated the code. Please check that first.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Originally Posted by Sachin Bali
    I already updated the code. Please check that first.
    copy/paste the code in a compiler, there are still errors. I know cuz I checked. copy/pasted it in codeblocks and there's 5-6 errors, although the concept is clear. I can see what you did there...
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by zedeneye1
    copy/paste the code in a compiler, there are still errors. I know cuz I checked. copy/pasted it in codeblocks and there's 5-6 errors, although the concept is clear. I can see what you did there...
    Don't copy paste and execute the code. My work was to write algorithm and to clear the concept. Its the user responsibility to use it and to take care of the syntactical mistakes.


    Don't take it as confirm code. Take it as a algorithm.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Originally Posted by Sachin Bali
    Don't copy paste and execute the code. My work was to write algorithm and to clear the concept. Its the user responsibility to use it and to take care of the syntactical mistakes.


    Don't take it as confirm code. Take it as a algorithm.
    :thumbs:
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    reverse string in c


    Thank You Very Much..:)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo