|
|
| Welcome to the Mac Forums forums. Please read the FAQ if you have questions. Register to participate. |
|
|||||||
| TouchArcade.com - iPhone Game Reviews and News |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
macrumors member
Join Date: Jan 2009
|
[C programming] reverse string
Alright, I'm having another problem in C. I want to make a program that takes a string inputted by the user and reverses it. I got it to work, but it displays gibberish at the start, which I can't get rid of.
Here is the function prototype: Code:
void reverse(char string[], int size); Code:
#include <stdio.h>
void reverse(char string[], int size)
{
int count = size -1;
for (count; count >= 0; count--)
{
if (string[count] >= 33 && string[count] < 123| string[count] >= 65 && string[count] < 123 | string[count] >= 97 && string[count] < 123)
{
printf("%c", string[count]);
}
}
printf("\n");
}
Code:
#include <stdio.h>
#define Size 700
int main()
{
char sentence[Size];
int items = Size;
printf("Enter a string: ");
fgets(sentence, Size, stdin);
reverse(sentence, items);
return 0;
}
Last edited by gotenks05 : Nov 4, 2009 at 12:58 PM. Reason: wrong function mentioned |
|
|
|
|
|
#2 |
|
macrumors regular
Join Date: Oct 2008
Location: Achewood, CA
|
You're not just printing the bytes of the string in reverse order; you're printing the bytes of the entire 700-byte buffer in reverse order.
I see that you've tried to compensate for this by trying to print only characters with certain ASCII values. However, when your program starts, your 700-byte buffer might not be all zeros; it'll probably be filled with random values, some of which will probably be letters. These gibberish values don't get overwritten by fgets(), so they'll be printed in your reverse() function. So you shouldn't be passing 700 as the size parameter to reverse(). Use the actual length of the string the user entered. This can be computed with the strlen() function. Also, technically, you're not reversing the string, you're printing the letters in reverse order. The characters in the string haven't moved anywhere. Actually reversing the bytes a string is a good exercise; do it without making a copy of the string for bonus points! ;-)
__________________
*** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool |
|
|
|
| autorelease |
| View Public Profile |
| Find More Posts by autorelease |
|
|
#3 | |
|
Thread Starter
macrumors member
Join Date: Jan 2009
|
Quote:
|
|
|
|
|
|
|
#4 |
|
macrumors 6502a
Join Date: Nov 2002
Location: MI
|
Also, your conditional is not doing what you think it is. It's simply accepting all characters between 33 (inclusive) and 123. I'd recommend checking for yourself to see why it's not doing what you think it is. However, following autorelease's advice, you won't even need this test to print valid data.
__________________
We start, then, with nothing, pure zero. But this is not the nothing of negation. For not means other than, and other is merely a synonym of the ordinal numeral second. As such it implies a first; while the present pure zero is prior to every first. The nothing of negation is the nothing of death, which comes second to, or after, everything. But this pure zero is the nothing of not having been born. There is no individual thing, no compulsion, outward nor inward, no law. It is the germinal nothing, in which the whole universe is involved or foreshadowed. As such, it is absolutely undefined and unlimited possibility -- boundless possibility. There is no compulsion and no law. It is boundless freedom. -Charles S. Peirce www.nothing.com |
|
|
|
|
|
#5 |
|
macrumors regular
Join Date: May 2009
Location: Seattle
|
Since he's implied a solution but not posted it I'll throw this one down for future readers.
Assuming C99 and 'char string[]' is NOT a pointer to a string constant - Code:
void reverse(char string[])
{
char* left = &string[0]-1;
char* right = &string[strlen(string)];
char temp;
while ( ++left < --right )
{
temp = *left;
*left = *right;
*right = temp;
}
}
Last edited by lloyddean : Nov 5, 2009 at 12:33 PM. Reason: Rewrote the function to work correctly. |
|
|
|
|
|
#6 | |
|
macrumors 68040
Join Date: Nov 2005
|
Quote:
Question: What happens when you pass in a string with 2, 4, 6 or 8 characters? |
|
|
|
|
| gnasher729 |
| View Public Profile |
| Find More Posts by gnasher729 |
|
|
#7 | |
|
macrumors member
Join Date: Jul 2002
|
Quote:
Code:
void reverse(char string[])
{
for (int low = 0, high = strlen(string)-1; low < high; ++low, --high)
{
const char temp = string[low];
string[low] = string[high];
string[high] = temp;
}
}
|
|
|
|
|
| JangoFett124 |
| View Public Profile |
| Find More Posts by JangoFett124 |
|
|
#8 |
|
macrumors regular
Join Date: May 2009
Location: Seattle
|
Whoops you're right gnasher729.
|
|
|
|
|
|
#9 |
|
macrumors 6502a
Join Date: Nov 2002
Location: MI
|
lloyddean, it doesn't matter for efficiency, and it shouldn't introduce any bugs, but you don't have to execute your loop when low and high are equal.
__________________
We start, then, with nothing, pure zero. But this is not the nothing of negation. For not means other than, and other is merely a synonym of the ordinal numeral second. As such it implies a first; while the present pure zero is prior to every first. The nothing of negation is the nothing of death, which comes second to, or after, everything. But this pure zero is the nothing of not having been born. There is no individual thing, no compulsion, outward nor inward, no law. It is the germinal nothing, in which the whole universe is involved or foreshadowed. As such, it is absolutely undefined and unlimited possibility -- boundless possibility. There is no compulsion and no law. It is boundless freedom. -Charles S. Peirce www.nothing.com |
|
|
|
|
|
#10 | |
|
macrumors 68000
Join Date: Jan 2005
Location: Austin, TX
|
Quote:
-Lee |
|
|
|
|
|
|
#11 | |
|
macrumors regular
Join Date: Oct 2008
Location: Achewood, CA
|
Quote:
Here's my solution to the in-place swap I suggested. JangoFett and lloyddean pretty much had it right, I just prefer for loops ![]() Code:
void reverse(char *str)
{
char *front, *back;
for (front = str, back = str+strlen(str)-1; front < back; front++, back--)
{
char tmp = *front;
*front = *back;
*back = tmp;
}
}
__________________
*** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool |
|
|
|
|
| autorelease |
| View Public Profile |
| Find More Posts by autorelease |
|
|
#12 | |
|
macrumors 68000
Join Date: Jan 2005
Location: Austin, TX
|
Quote:
Code:
void reverse(char *str)
{
char *front, *back;
int isPalindrome = 1;
for (front = str, back = str+strlen(str)-1; front < back; front++, back--)
{
if(*front != *back) {
char tmp = *front;
isPalindrome = 0;
*front = *back;
*back = tmp;
}
}
if(isPalindrome == 1) printf("Found a palindrome! Huzzah!\n");
}
-Lee |
|
|
|
|
|
|
#13 | |
|
macrumors regular
Join Date: May 2009
Location: Seattle
|
Quote:
But, I think it's best left as a curiosity on todays desktop systems and game consoles. |
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|