Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 17th, 2003, 12:59 PM
 Questioner
Contributing User

Join Date: Jul 2003
Location: Questioner's home
Posts: 89
Time spent in forums: 8 h 32 m 10 sec
Reputation Power: 11
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.

#2
July 17th, 2003, 01:57 PM
 joegluntz
Junior Member

Join Date: Jul 2003
Posts: 10
Time spent in forums: < 1 sec
Reputation Power: 0
You can show your indentations using the PHP formatting tags.

#3
July 17th, 2003, 02:46 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,882
Time spent in forums: 3 Months 1 Day 14 h 51 m 6 sec
Reputation Power: 2199
Re: C function error?

Quote:
 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 02:52 PM.

#4
July 17th, 2003, 02:56 PM
 3dfxMM
Contributing User

Join Date: Aug 2002
Posts: 272
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 18
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.

#5
July 20th, 2003, 04:04 PM
 Questioner
Contributing User

Join Date: Jul 2003
Location: Questioner's home
Posts: 89
Time spent in forums: 8 h 32 m 10 sec
Reputation Power: 11
FIXED!!!

PHP Code:
```  char* IRS(char* str) {     char* InRev;     char* Reversed;     char* str2;     int len = 0, i, j = 0, k = 0;     for(i = 0; str[i] != '\0'; i++)     {         len++;     }     InRev = (char*) malloc(2 * len + 1);     str2 = (char*) malloc(len + 1);     for(i = 0; i < len; i++)     {         str2[i] = str[i];     }     str2[ i ] = '\0';     Reversed = RS(str2);     for(i = 0; i < 2 * (len + 1); i++)     {         if(i % 2 != 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.
__________________
What can change the nature of a man?

Last edited by Questioner : July 20th, 2003 at 04:13 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > C function error?