Thread: string reverse

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    string reverse

    i am unable to find the defect in the prog of string reverse without using a third varaible,
    Code:
    # include<stdio.h>
    # include<conio.h>
    # include<string.h>
    void main()
    {
    char name[20];
    int c1=0,c2;
    clrscr();
    printf("enter the string");
    scanf("%s",name);
    c2=strlen(name);
    printf("\n %d \n",c2);
    c2--;
    for( ;c1++<c2--;)
    {
    name[c1]=name[c1]+name[c2];
    name[c2]=name[c1]-name[c2];
    name[c1]=name[c1]-name[c2];
    }
    printf("the reversed string \n");
    printf("%s",name);
    getch();
    }
    If i am giving input abc it is giving a,plz help me
    Last edited by Salem; 09-06-2004 at 02:18 AM. Reason: tagging and smilies - please review and correct before submitting in future

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb Try This

    To see whats going on, try this:
    Code:
    # include<stdio.h>
    # include<conio.h>
    # include<string.h>
    void main()
    {
    char name[20];
    int c1=0,c2;
    clrscr();
    printf("enter the string");
    scanf("%s",name);
    c2=strlen(name);
    printf("\n %d \n",c2);
    c2--;
    printf("C1 = %d\tC2 = %d\n",c1,c2);
    for( ;c1++<c2--;)
    {
    name[c1]=name[c1]+name[c2];
    printf("Iteration 1: name[c1] = %d\t",name[c1]);
    name[c2]=name[c1]-name[c2];
    printf("name[c2] = %d\t",name[c2]);
    name[c1]=name[c1]-name[c2];
    printf("name[c1] = %d\n",name[c1]);
    }
    printf("Finally C1 = %d\t C2 = %d\n",c1,c2);
    printf("the reversed string \n");
    printf("%s",name);
    getch();
    }
    Hope it makes your analysis clearer...

    -Harsha
    Help everyone you can

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vsriharsha
    To see whats going on, try this:
    Code:
    # include<stdio.h>
    # include<conio.h>
    # include<string.h>
    void main()
    {
    char name[20];
    int c1=0,c2;
    clrscr();
    printf("enter the string");
    scanf("%s",name);
    c2=strlen(name);
    printf("\n %d \n",c2);
    c2--;
    printf("C1 = %d\tC2 = %d\n",c1,c2);
    for( ;c1++<c2--;)
    {
    name[c1]=name[c1]+name[c2];
    printf("Iteration 1: name[c1] = %d\t",name[c1]);
    name[c2]=name[c1]-name[c2];
    printf("name[c2] = %d\t",name[c2]);
    name[c1]=name[c1]-name[c2];
    printf("name[c1] = %d\n",name[c1]);
    }
    printf("Finally C1 = %d\t C2 = %d\n",c1,c2);
    printf("the reversed string \n");
    printf("%s",name);
    getch();
    }
    Hope it makes your analysis clearer...

    -Harsha
    Here are some additional points of interest:

    1) The entirely unneeded, and non-standard <conio.h> header. Seriously, what's the point, when by typing 16 less characters, you can use getchar and have your program portable?

    2) The incorrect declaration and usage of the main. Because as all good FAQ readers know, main returns an int.

    3) The usage of scanf for reading strings, because as this FAQ entry shows, there are much nicer ways to get a line from the user.

    4) And finally, what's the point of using [code] tags if you don't bother indenting?


    Quzah.
    Last edited by quzah; 09-06-2004 at 03:48 AM.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i am unable to find the defect in the prog of string reverse without using a third varaible,
    Perhaps because there are half a dozen (or more defects)
    In addition to those already mentioned

    1. for( ;c1++<c2--
    c1 and c2 change before you get to do any swapping of variables.

    2. name[c1]=name[c1]+name[c2];
    Since you don't know whether char is unsigned or signed, you have no way of knowing whether overflow in the addition will wrap around correctly, or invoke undefined behaviour.

    Besides, swapping without a temporary ceased to be interesting when we stopped using assembler. All you end up doing is confusing either the reader or the compiler (or both)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM