Thread: Question about functions

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Question about functions

    I tried to make a code reverser like to reverse ur name from like David to divad
    but it keeps getting same name not reversed
    Code:
    #include <stdio.h>
    char namee(char name[])
    {
    	int x;
    	for(x=5;x<=0;x--)
    		;
    	return 0;
    }
    int main(void)
    {
    char name[5];
    fputs("please enter your name: ",stdout);
    fgets(name,6,stdin);
    namee(name);
    printf("Now name reversed is %s",name);
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Maybe you should put something in the for loop, rather than doing absolutely nothing (well you're counting down from 5 to 0, but that's not that exciting).

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh yah forgot about name[x] lolz
    but still didnt work out:S
    Code:
    #include <stdio.h>
    char namee(char name[])
    {
    	int x;
    	for(x=5;name[x]<0;x--)
    		;
    	return 0;
    }
    int main(void)
    {
    char name[5];
    fputs("please enter your name: ",stdout);
    fgets(name,6,stdin);
    namee(name);
    printf("Now name refersed is %s",name);
    return 0;
    }
    but it still didnt work out

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you expect name[x], which is a letter, to be less than zero? Really?

    And even if it is, do you (still) expect your for loop to actually do anything?

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    why it run from 5 then -- to 0 ? so it will run reverse the name ?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Right now it doesn't run.

    The first version did run, at least. It didn't do anything, but it would run. The second version, if it ran, wouldn't do anything either. If you want to do something in the function, like change name, you should probably do so instead of hoping it happens somehow.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like using a left and right variable, together with a temp char, and swap them while left is less than right. Left being started at 0 and right being started at the last letter.

    Since the char array may very well be larger than the name it's holding, I use something like this, to set right up, before the for loop that makes the exchange:

    Code:
    right = Max; // where Max is the size of the name array
    while(name[--right] == '\0');
    That puts right, right on the last letter, no matter the number of char's in name, or the size of the name array. Note the semi-colon at the end of that line.

    The swap itself is the same as a swap in a sort:

    temp = name[left]
    name[left] = name[right]
    name[right] = temp
    --right;

    left is incremented in the for loop itself.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    finally did it thanks adak for poiting me to some stuff
    Code:
    #include <stdio.h>
    #include <string.h>
    input(){
    	char name[5];
    int i;
    fputs("Please Enter your name: ",stdout);
    fgets(name,6,stdin);
    for (i=strlen(name); i >= 0; i--) { 
    	printf("%c", name[i]);
    }
    }
    int main(void)
    {
    input();
    return 0;
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  4. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 09:39 AM