#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    12

    C function error?


    I have another question, I think this one's more tidy then my previous question. I think i get the logics right, just i dunno why it wont show me the correct output.

    This function is suppose to accept a string from user, then convert it to something else, for example, if the user enter "abc", then it will show "acbbca". The original string is reversed, and inserted into the original string one character after the other.

    This function keep showing me "ccb" instead of "acbbca".

    char* IRS(char* str)
    {
    char* InRev;
    char* Reversed;
    int len = 0, i, j = 0, k = 0;

    for(i = 0; str[i] != '\0'; i++)
    {
    len++;
    }

    InRev = (char*) malloc(2 * len + 1);

    Reversed = RS(str);

    for(i = 0; i < len; i++)
    {
    if(i % 2 != 1)
    {
    InRev[i] = str[j];
    j++;
    }
    else
    {
    InRev[i] = Reversed[k];
    k++;
    }
    }

    InRev[i] = '\0';

    return InRev;
    }

    The procedure below is correct, I put it here becoz it's called in the function above.

    char* RS(char* str)
    {
    char* start = str;
    char* end = 0;
    int len = 0;
    char t = '\0';

    while ((*start++))
    {
    len++;
    }

    start = str;
    end = (str + len - 1);

    while (start < end)
    {
    t = (*end);
    (*end) = (*start);
    (*start) = t;
    start++;
    end--;
    }
    return str;
    }

    Also, how do I show my indentations correctly in this forum? They seemed to ignore the tabs.

    Thanks for your help.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    10
    Rep Power
    0
    You can show your indentations using the PHP formatting tags.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222

    Re: C function error?


    Originally posted by Questioner

    ...
    Also, how do I show my indentations correctly in this forum? They seemed to ignore the tabs.

    Thanks for your help.
    You can use either php or code tags. code just preserves the indentation, but php apparently also uses different text colors based on the syntax.

    Tags are almost exactly like HTML, except that square brackets are used; ie, '[' and ']'. You start the code section with the code tag and end it with the /code tag. Reply to this message and look at the "Automatically parse URLs" option below your edit window; there you will see url tags.

    You can also bold (with b tags), italisize (with i tags), and underline (with u tags). You can also quote somebody with quote tags.

    BTW, I should offer you a heads-up on one problem I keep having with tags. When I italisize after a code listing and that code listing contains an array indexed by the variable i (eg, "arry[i]"), then that "[i]" is interpreted as the beginning of the italisized text. I've had to change the code to read "arry[ i ]" instead.
    Last edited by dwise1_aol; July 17th, 2003 at 01:52 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    You are reversing the string in place which is destroying the original string. In other words, Reversed and str point to the same string, the reversed one. Also, you need to go through your loop that merges the strings the length of the merged string, not the original string length.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    12
    FIXED!!!

    PHP Code:
    charIRS(charstr)
    {
        
    charInRev;
        
    charReversed;
        
    charstr2;
        
    int len 0i00;

        for(
    0str[i] != '\0'i++)
        {
            
    len++;
        }

        
    InRev = (char*) malloc(len 1);

        
    str2 = (char*) malloc(len 1);

        for(
    0leni++)
        {
            
    str2[i] = str[i];
        }

        
    str2] = '\0';

        
    Reversed RS(str2);

        for(
    0* (len 1); i++)
        {
            if(
    != 1)
            {
                
    InRev[i] = str[j];
                
    j++;
            }
            else
            {
                
    InRev[i] = Reversed[k];
                
    k++;
            }
        }

        
    InRev[i] = '\0';

        return 
    InRev;

    Thanks a lot! That "for(i = 0; i < 2 * (len + 1); i++)" part is really a blind spot, if without being told by someone, I wont be able to spot it myself. The detailed explaination of tags is helpful as well.
    Last edited by Questioner; July 20th, 2003 at 03:13 PM.
    What can change the nature of a man?

IMN logo majestic logo threadwatch logo seochat tools logo