Thread: Problem in name reverser

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Problem in name reverser

    Code:
    #include <stdio.h>
    #include <string.h>
    void reverse_it(char *buffer,int num)
    {
         int i;
         for(i=num;buffer[i]>=0;i--);
    }
    int main(void)
    {
        char name[]="KARIM";
        reverse_it(name,strlen(name));
        puts(name);
        getchar();
        return 0;
    }
    i dunno what the problem my code be bad.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Your reverse function isn't doing anything. There is no statement that changes the buffer. Plus, the for loop isn't correct. You don't want buffer[i]>=0, but i>=0.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverse_it(char *buffer,int num)
    {
         int i;
         for(i=num;buffer[i]>=0;i--);
    }
    int main(void)
    {
        char name[]="KARIM";
        reverse_it(name,strlen(name));
        puts(name);
        getchar();
        return 0;
    }
    i dunno what the problem my code be bad.
    put a condition to reverse the string in the for loop statement instead of putting a semicolon after it.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    now it works but after the loop is done it doesnt work does that mean it doesnt change the hole thing ?
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverse_it(char *buffer,int num)
    {
         int i;
         for(i=num;i>=0;i--){
             printf("%c",buffer[i]);
             }
    }
    int main(void)
    {
        char name[]="KARIM";
        reverse_it(name,strlen(name));
        puts(name);
        getchar();
        return 0;
    }

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    now it works but after the loop is done it doesnt work does that mean it doesnt change the hole thing ?
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverse_it(char *buffer,int num)
    {
         int i;
         for(i=num;i>=0;i--){
             printf("%c",buffer[i]);
             }
    }
    int main(void)
    {
        char name[]="KARIM";
        reverse_it(name,strlen(name));
        puts(name);
        getchar();
        return 0;
    }
    yes.the actual name hasn't been changed.u're simply printing the name in reverse order in the reverse function.and after it comes to the main function it prints its original name.if u want to store reverse of KARIM in name u've to take another variable in reverse function that changes the order of name.u can also swap the characters in name in reverse function without using another variable.
    Last edited by BEN10; 04-21-2009 at 07:34 AM.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by BEN10 View Post
    yes.the actual name hasn't been changed.u're simply printing the name in reverse order in the reverse function.and after it comes to the main function it prints its original name.if u want to store reverse of KARIM in name u've to take another variable in reverse function that changes the order of name.u can also swap the characters in name in reverse function without using another variable.
    how can i reverse it in whish last char comes first and the one after it comes after it and like that .?

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    how can i reverse it in whish last char comes first and the one after it comes after it and like that .?
    as i said just swap the last and first char and 2nd and the 2nd last char uptil the mid char.this way the string will be reversed.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by BEN10 View Post
    as i said just swap the last and first char and 2nd and the 2nd last char uptil the mid char.this way the string will be reversed.
    okay thanks i will try

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i did it but program crashed
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverse_it(char *buffer,int length)
    {
         int i,x;
         char save;
         for(i=0,x=length;;i++,x--){
             buffer[i]=buffer[x];
            }
    }
    int main(void)
    {
        char name[]="NAME";
        reverse_it(name,strlen(name));
        puts(name);
        getchar();
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    You need a test in the for loop, so it knows when to quit.
    Last edited by Adak; 04-21-2009 at 02:19 PM.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i made i<x but i have no idea why is that but it worked also the name reverser doesnt work that well like name will be EMME and when i added x=length-1 it worked but x=length doesnt work can someone explain why ? thanks alot
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverse_it(char *buffer,int length)
    {
         int i,x;
         
         for(i=0,x=length;i<x;i++,x--){
             buffer[i]=buffer[x];
            }
    }
    int main(void)
    {
        char name[]="NAME";
        int i;
        reverse_it(name,strlen(name));
        puts(name);
        getchar();
        return 0;
    }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    Lengths begin with one, but arrays begin at a zero index, not one.

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    and why does it not work properly ? i mean name is now EMME ??

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a hint: How do you swap two variables?

    Once you figure that out, your problems will almost be solved.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by quzah View Post
    Here's a hint: How do you swap two variables?

    Once you figure that out, your problems will almost be solved.


    Quzah.
    you swap it using a 3rd variable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM