473,791 Members | 2,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String reversing problem

Why doesn't:

#include <stdio.h>

void reverse(char[], int);

main()
{
char s[5];

s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
reverse(s, 5);

for (int i=0; i<=4; i++)
putchar(s[i]);
return 0;
}

void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
s[i] = s[j];
}

output:

olleh

?

Dec 30 '05 #1
46 2153
Albert wrote:
Why doesn't:

#include <stdio.h>

void reverse(char[], int);

main()
{
char s[5];

s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
reverse(s, 5);

for (int i=0; i<=4; i++)
putchar(s[i]);
return 0;
}

void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
Think what happens when i==4 and j==1, for example. [Do they still call
it `desk checking'?]
s[i] = s[j];
}

output:

olleh

?

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 30 '05 #2
use this one
#include <stdio.h>
void reverse(char[], int);
main()
{
char s[5];
int i;

s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
reverse(s, 5);
for ( i = 0; i<=4; i++)
putchar(s[i]);
return 0;

}
void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
s[i] = s[j];

}

Dec 30 '05 #3
What do you mean by 'desk checking'?

Dec 30 '05 #4
Albert wrote:
What do you mean by 'desk checking'?


I think he means checking by pen & paper. I'd call it "checking by pen
& paper" although I always use a whiteboard for it.

Computers can only do what you tell them to do. As they say: garbage
in, garbage out. Sometimes when the computer doesn't do what you want
it is worth checking if you told it to do what you thought you wanted.

Dec 30 '05 #5
Albert wrote:
Why doesn't:

<snip>
s[i] = s[j];


This is a good example of why desk-checking is good. Lets take a
"hello" string shall we? Note that in the "diagram" below I mark the
variable being "read" from with + and the variable being "written" to
with ^.

h e l l o // original string

o e l l o // doing s[0] = s[4]
^ +

o l l l o // doing s[1] = s[3]
^ +

o l l l o // doing s[2] = s[2]
^

o l l l o // doing s[3] = s[1]
+ ^

At this point I hope you see the problem with you code since you've
overwritten the original 'e' with an 'l'.

Dec 30 '05 #6
pai
hi ,
I think an extra variable is needed to store, bcoz u cant
interchange 2 variable as such.
or is there any way to do it ...
Pai

Dec 30 '05 #7
pai said:
hi ,
I think an extra variable is needed to store, bcoz u cant
interchange 2 variable as such.
or is there any way to do it ...


There is a way to do this under certain conditions, but it's not a very
bright idea.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 30 '05 #8
pai wrote:

I think an extra variable is needed to store, bcoz u cant
interchange 2 variable as such. or is there any way to do it

Include context, without which your message is meaningless. For
means on the broken google interface, see my sig below.

Try this, after #include <string.h>:

/* reverse string in place. Return length */
static size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

if ((lgh = strlen(stg)) > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Dec 30 '05 #9

Albert wrote:
Why doesn't: [...] void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
s[i] = s[j];
}


try writing reverse(...) like this:

/***/
void reverse(char s[], int num_elements)
{
int i, j;
char t;

for (i=0,j=num_elem ents-1; (i<=(num_elemen ts-1) / 2) && (j>=0);
i++,j--)
{
t = s[i];
s[i] = s[j];
s[j] = t;
}
}

/***/

then analyze both to figure out whats the difference and what happend.

Dec 30 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
5324
by: Rakesh | last post by:
Hi, I have this function to reverse the given string. I am just curious if that is correct and there could be better way of doing it / probable bugs in the same. The function prototype is similar to the one in any standard C library. <---- Code starts -->
24
3284
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include <string.h> char* StrReverse(char*);
5
1352
by: Shwetabh | last post by:
Hi, How can I remove a part of string from complete string in SQL? this is something i want to do: update ace set PICTURE = PICTURE - '\\SHWETABH\Shwetabh\I\' But here ' - ' is not allowed.
41
3388
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
4
2415
by: Jonathan Wood | last post by:
Is it just me? It seems like one moving from MFC to C# loses some string functionality such as the two mentioned in the subject. Did I miss something? -- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com
2
12070
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code ..but it seems a bit "bloated" as i need an extra array of same size of the original string
16
2091
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it should except that every traversal thru the string I've gotten it to repeat the "list" again. This is what it looks like: for char in x: mylist.append(char)
3
6104
by: steezli | last post by:
Hi, Brand new to VB.NET and I'm having a problem figuring out this program. I'll try and be descritive as possible. I have to create a Windows application that contains a single top-level form with two textboxes on it, on positioned above the other. As each character is entered into the upper textbox, the string that has been entered into the upper textbox must appear in the lower textbox, but in reverse. If the input field contains...
1
3313
by: rajkumarbathula | last post by:
Hi Could any one help me out in reversing rows/elements of DataTable or String or DataList by using any simple statement? Thanks
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.