Thread: Character array problem

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    Character array problem

    I am trying to solve this problem from K&R book.

    Write a function reverse(s) that reverses the character string s. Use it to
    write a program that reverses its input a line at a time.
    My solution
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLINE 10
    
    
    void reverse(char r[], char s[]);
    
    int main(void) {
    
    	int i,c;
    	char line[MAXLINE];
    	char reversed[MAXLINE];
    
    	for (i=0; i < MAXLINE && (c=getchar())!='X' && c!='\n'; ++i)
    		line [i] = c;
    	reverse (reversed,line);
    
    
    	printf ("%s", reversed);
    
    		return 0;
    }
    
    
    
    
    void reverse (char r[], char s[])
    {
    		int i=0;
    
    		for (i=0; i<=10; i++)
    			r[i]=0;
    
    		while (i <= 10 && s[i] != '\0')
    {
    		r[10-i] = s[i];
    
    		}
    		++i;
    }
    The problem is...am not getting any output. I am guessing it's a concept problem with me but I am unable to figure it out. Any help please?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    After this for-loop
    Code:
    for (i=0; i<=10; i++)
        r[i]=0;
    i is 11.

    Now look at the next line:
    Code:
    while (i <= 10 && s[i] != '\0')
    The body of the while loop will never be executed.

    You also have a buffer overflow because your valid indices are 0-9, thus you can't check if i <= 10.

    And you should use the macro instead of 10.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    How do I use macro in the function, I may get an 'undefined variable error' (apologies if I am missing on something)?

    Thanks for pointing it out. My minor tweaked code is as under, pasting it entirely, still no output:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLINE 10
    
    
    void reverse(char r[], char s[]);
    
    int main(void) {
    
    	int i,c;
    	char line[MAXLINE];
    	char reversed[MAXLINE];
    
    	for (i=0; i < MAXLINE && (c=getchar())!='X' && c!='\n'; ++i)
    		line [i] = c;
    	reverse (reversed,line);
    
    
    	printf ("%s", reversed);
    
    		return 0;
    }
    
    
    
    
    void reverse (char r[], char s[])
    {
    		int i=0;
    		int p=0;
    
    		for (p=0; p<10; i++)
    			r[p]=0;
    
    
    		while (i < 10 && s[i] != '\0'){
    		r[9-i] = s[i];
    
    		}
    		++i;
    }
    Last edited by alter.ego; 09-04-2012 at 09:48 AM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
            for (p=0; p<10; i++)
                r[p]=0;
    You're still incrementing i instead of p.
    Last edited by itsme86; 09-04-2012 at 10:06 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by alter.ego View Post
    How do I use macro in the function, I may get an 'undefined variable error' (apologies if I am missing on something)?
    You already have used the MAXLINE macro in main when you created the arrays. You should have no problem using it in the loop to replace the magic numbers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Whoops! HOW DID I MISS IT!!!?

    Also I did an elementary mistake of keeping ++i outside the loop & it kept getting reseted to 0. Code works now!


    Thanks hk_mp5kpdw, itsme86 & AndiPersti for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character array problem .. please help :((
    By amerjamil in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2010, 04:02 AM
  2. Character array initialization problem
    By ThatDudeMan in forum C Programming
    Replies: 7
    Last Post: 12-03-2010, 03:56 PM
  3. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  4. Problem with setting a character array in a class member.
    By swbluto in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2006, 02:41 AM
  5. Problem with moving character around(using array for map)
    By o0obruceleeo0o in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2003, 05:29 AM