Like Tree1Likes
  • 1 Post By quzah

reverse a string without string.h library

This is a discussion on reverse a string without string.h library within the C Programming forums, part of the General Programming Boards category; can anyone help me with my exercise.. i want to inverse a string but without using any of string.h' s ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    reverse a string without string.h library

    can anyone help me with my exercise.. i want to inverse a string but without using any of string.h' s library functions

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    What have you tried so far? This isn't a homework service. The basic answer is that you have to treat a string for what it is, a char array, and then iterate through the array in reverse.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    well i have tried this so far..


    Code:
    void reverse(char str[])
    {
    int i=0, j=0;
    
    printf("Enter String (up to 50 chars): ");
    scanf("%s", str);
    
    
    while(str[j] != '\0')
    {
    
    j++;
    //j--;
    
    }
    
    while(i<=j)
    {
    
    char t = str[i];
    
    str[i++] = str[j];
    str[j--] = t;
    
    }
    
    printf("%s", str);
    
    return;
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Your attempt doesn't make sense. Ask for and then get the string in main. Then pass that to your reverse function. This is a pretty basic question, search this board for "surname swap", we had a good discussion of it there. Note that since the NULL char evaluates to false for C, you can use that for string length. Additionally, there are methods of getting user input that will return the number of characters read in.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thanks a lot! Well i have managed to build this part until now but i have to figure a way to count the length of the string without using string.h

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    antros48 likes this.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by antros48 View Post
    Thanks a lot! Well i have managed to build this part until now but i have to figure a way to count the length of the string without using string.h
    Length counting is easy --and provides a real good hint on the rest of the problem as well...

    Code:
    int GetLength(char *str)
      { int i = 0;
        while ( str[ i++ ] );
        return i - 1; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse a string
    By GeorgeOfTheBush in forum C Programming
    Replies: 1
    Last Post: 12-26-2009, 01:24 PM
  2. [Help] About reverse string
    By kingofdcp in forum C Programming
    Replies: 3
    Last Post: 06-06-2009, 03:53 AM
  3. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  4. string reverse
    By c_cool in forum C Programming
    Replies: 3
    Last Post: 09-06-2004, 07:41 AM
  5. How do I reverse a string?
    By Cshot in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 08-06-2002, 06:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21