Thread: reversing the content of array character-wise

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    reversing the content of array character-wise

    I am a newbie and havent read pointers yet. This programme breaks in the function when it is supposed to reverse the characters.e-g "four" --> "ruof"

    Herre is what i did.
    I created another array of same size to copy the contents of first array and was thinking of for loop going from the length of character to the zero subscript and print those values.

    Any help would be appreciated. All i am interested in is what should be the right for loop with appropriate condition to put the characters on screen in reverse.


    Code:
    #include <stdio.h>
    #define MAX_SIZE 10
    
    int string_length_func(char string_data[],char reverse_string[]); //func prorotype
    
    int main(void)
    {
    	/* Declare a character array to store the string in */
    	
    	char string_data[MAX_SIZE];
    	char reverse_string[MAX_SIZE];
    
    	int length_word;
    
    	/* Ask the user for a string */
    	printf("Enter a word: ");
    	scanf("%9s", string_data);
    	
            length_word = string_length_func(string_data,reverse_string);
    	
    	return 0;
    }
    
    int string_length_func(char string_data[],char reverse_string[])
    {
    	char reverse_word=5;
    	int element_num =0;
    
    for (element_num = 0; 
    	(element_num < MAX_SIZE) && !(string_data[element_num] == '\0'); 
    	element_num ++)
    	{
    		reverse_string[element_num] = string_data[element_num];
    	}
    
    for( reverse_word = --element_num;(reverse_word <= element_num) && (reverse_word >=0);reverse_word --)
    
    {
    	printf("%s\t",reverse_string[element_num]);
    }
    
    return (element_num);
    }
    Last edited by bigjoke; 12-25-2005 at 04:09 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do people overcomplicate things so much?
    Code:
    for( x = start; fromhere[ x ]; x++ )
        tohere[ x ] = fromhere[ end - x - 1 ];
    Oh, and you will need to null terminate your new string. Here's a Christmas present:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    void revs( char fromhere[], char tohere[] )
    {
        size_t x;
        
        for( x = 0; fromhere[ x ]; x++ )
            tohere[ x ] = fromhere[ strlen( fromhere ) - x - 1 ];
        tohere[ x ] = '\0';
    }
    
    int main( void )
    {
        char a[BUFSIZ] = "Hello World!";
        char b[BUFSIZ] = {0};
        
        revs( a, b );
        
        puts( a );
        puts( b );
        
        return 0;
    }
    If you don't like Christmas, too bad, here's a present anyway.



    Quzah.
    Last edited by quzah; 12-25-2005 at 04:16 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    You have used few functions like strlen() etc that i have not ever come across before.

    I would really appreciate if you could explain it to me down at my level and If possible, in terms of the variables i have already used.

    Meanwhile, i am trying to make sense of what you have already told.

    thanks for reply quzah

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    strlen()

    what are the other functions?
    also, a quick search on google will yield results for explanations of the functions :]
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you don't have to have strlen in there. All it does is calculate the length of a string. You can make your own. Simply walk through a string character by character, and count until you reach the null. Don't count it.
    Code:
    size_t mystrlen( char *s )
    {
        size_t len = 0; /* size_t is a variable type, unsigned,
                           which is safe to use for array indexing */
        for( len = 0; s[ len ]; len++ ) /* for each letter, stop on null, increment len */
            ; /* don't do anything, just count */
    
        return len; /* return the length */
    }
    There are many ways to do it, that's just a simple version to learn by.

    Anyway, my code was basically this:
    Code:
    for each letter in the 'fromhere' string, stopping when we reach the end do:
        take this position, noted by x, of the 'tohere' string, and fill it with
        the character at 'the end' - x - 1
    
    now add a null on to the end of 'tohere'
    That is to say, if a string is 10 characters in length, not counting its null, do this:
    Code:
    tohere[ x ] = fromhere[ 10 - x - 1 ];
    Or:
    Code:
    foo[ 0 ] = bar[ 10 - 0 - 1 ];
    Which translates to:
    Code:
    foo[ 0 ] = bar[ 9 ];
    We subtract 1, because array indexing begins at zero, not 1. Naturally you repeat this throughout:
    Code:
    foo[0] = bar[9];
    foo[1] = bar[8];
    foo[2] = bar[7];
    ...
    foo[7] = bar[2];
    foo[8] = bar[1];
    foo[9] = bar[0];
    
    foo[ 10 ] = a null character, to terminate the string
    You can do the last part inside the loop if you change your loop conditions slightly. I'll leave that as an exercise to the reader, if you really feel like it.

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

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by willc0de4food
    what are the other functions?
    Probably puts. The puts function takes a string of characters, and sends it to the standard output device (commonly your screen), including a newline character. Thus, if the string is "foo", it will end up sending "foonewlinecharacter", where newlinecharacter is of course the newline character, and not the actual phrase newlinecharacter.


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

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Reversing a string in place:
    http://www.daniweb.com/code/snippet259.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    I have updated the above programme. Now, evrything is stored in the reversed array as i wanted it to be. But when i try to print the values the variable "element_num" goes to -1 alhough i have set up the condition in for loop to stop at zero. It still does so.
    As a result when printing it tries to print the character stored at element_num = -1 which is not possible for obvious limitation of indexes in array.

    What am i doing wrong. I am using ms visual c++ compiler.



    Code:
    /* Program to calculate the number of arrays and then reversing the order of character*/
    #include <stdio.h>
    #define MAX_SIZE 10
    
    int string_length_func(char string_data[]); //func prorotype
    
    int main(void)
    {	
    	char string_data[MAX_SIZE];
    	char reverse_string[MAX_SIZE];
    	
    	int length_word;
    	int element_num;
    	
    	/* Enter an array of letters*/
    	printf("Enter a word: ");
    	scanf("%9s", string_data);
    
    	/* Store total number of letters from this function in variable*/
        length_word = string_length_func(string_data);
    	
    	/* Print the variable*/ 
    	printf("This word contains %d letters\n",length_word);
    	
    	/* Now (trying to)reverse the letters*/
    	for (element_num = length_word; ((element_num < MAX_SIZE) && (element_num > -1));-- element_num )
    	{
    		reverse_string[element_num] = string_data [element_num];
    	}
    
    	/* printing the reversed letters stored in this array*/
     	printf("%s\t",reverse_string[element_num]);
    
    	return 0;
    }
    
    int string_length_func(char string_data[])
    {
    	int element_num =0;
    
    	for (element_num = 0; 
    		(element_num < MAX_SIZE) && !(string_data[element_num] == '\0'); 
    		element_num ++);
    
    		return (element_num);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 12:12 AM
  5. Reversing a Multi-D Array
    By Bradley33 in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 11:37 AM