print a string modified-what's wrong?

A place to discuss the science of computers and programs, from algorithms to computability.

Formal proofs preferred.

Moderators: phlip, Moderators General, Prelates

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

print a string modified-what's wrong?

Postby evinda » Fri Jun 21, 2013 11:37 am UTC

Hey guys???I hope you can help me to find my error at a code..
This is the code I have written:

Code: Select all

#include <stdio.h>
#include <string.h>

void func(char *p1,char *p2){
   char ch;
   while (p1<p2){
         ch=*p1;
         *p1=*p2;
         *p2=ch;
         p1++;
         p1--;
   }
}
       
int main()
{
   char a[8];
   printf("Give a string:\n");
   gets(a);
   puts(a);
   func(a,a+4);
   puts(a);
   return 0; 
}

What have I done wrong???? :shock:

Divinas
Posts: 57
Joined: Wed Aug 26, 2009 7:04 am UTC

Re: print a string modified-what's wrong?

Postby Divinas » Fri Jun 21, 2013 12:53 pm UTC

I'm not really sure what you want to do, but you're incrementing and then decrementing p1. I guess one of those operations should operate on p2, instead.

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

Re: print a string modified-what's wrong?

Postby evinda » Fri Jun 21, 2013 2:07 pm UTC

Oh yes,I am sorry...I wanted to decrement p2...Thank you very much!!!!! :mrgreen:

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

Re: print a string modified-what's wrong?

Postby evinda » Fri Jun 21, 2013 4:45 pm UTC

Right now I runned the program,givig as input the string VENICE...Why I got as output the result CINEVE and not CENIVE???

User avatar
jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: print a string modified-what's wrong?

Postby jaap » Fri Jun 21, 2013 5:08 pm UTC

evinda wrote:Right now I runned the program,givig as input the string VENICE...Why I got as output the result CINEVE and not CENIVE???

What do you think the output of the program is supposed to be when you give it for example "1234567" as input?

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: print a string modified-what's wrong?

Postby Xenomortis » Fri Jun 21, 2013 5:27 pm UTC

evinda wrote:What have I done wrong???? :shock:

As a rule, it helps to state what is supposed to happen when it's working correctly.
For instance, I have no idea what "func" is supposed to do. It appears to be an attempt to scramble part of a string.
Image

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

Re: print a string modified-what's wrong?

Postby evinda » Fri Jun 21, 2013 5:29 pm UTC

I don't know...I got confused right now...at the command while (p1<p2),what do we check????if *p1<*p2 or something else????? :shock:

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

Re: print a string modified-what's wrong?

Postby evinda » Fri Jun 21, 2013 5:30 pm UTC

I got this program at a test and I haven't understood yet what it does....

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: print a string modified-what's wrong?

Postby Xenomortis » Fri Jun 21, 2013 5:38 pm UTC

Person A:
"This is not working. What have I done wrong?"

Person B:
"Well, what is it supposed to do?"

Person A:
"I don't know! Help!"

p1 < p2 is comparing the pointers themselves, not the values they're pointing at.
So it's true whilst p1 is pointing to a lower address in the character array than p2.
Image

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

Re: print a string modified-what's wrong?

Postby evinda » Fri Jun 21, 2013 5:47 pm UTC

I had forgotten how it was exactly written at the test,so I had tried to remeber it and write it... but why do we always have 2 repetitions? :?

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: print a string modified-what's wrong?

Postby Xenomortis » Fri Jun 21, 2013 5:54 pm UTC

Of the loop?
Take a look at your input for the function.

(My best guess for this purpose of this function is that it's supposed to reverse a string. But that's a guess and the actual function is a fair ways off.)
Image

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

Re: print a string modified-what's wrong?

Postby evinda » Fri Jun 21, 2013 6:13 pm UTC

So,at the command *p1=*p2,the address p1 stays unchanged...Or am I wrong??? :?:

beojan
Posts: 166
Joined: Mon May 23, 2011 12:11 pm UTC
Location: Oxford / London, United Kingdom, Europe

Re: print a string modified-what's wrong?

Postby beojan » Sun Jun 23, 2013 4:11 pm UTC

I think the purpose of this code is to take a string, print it, then print it again with the first 5 characters reversed.
Please read the comments in the code below:

Code: Select all

#include <stdio.h>
#include <string.h>

void func(char *p1,char *p2){
   char ch;
   while (p1<p2){  // p1 and p2 are pointers, so incrementing them will cause them to point at successive characters in a string
         // an asterisk in front of a pointer allows it to be used as a variable, so the next three lines
         // swap the characters pointed to by p1 and p2
         ch=*p1;
         *p1=*p2;
         *p2=ch;
         //increments p1, and decrements p2
         p1++;
         p1--; // should be p2, I think.
   }
//so, as a whole, the above loop reverses the string between p1 and p2
}
       
int main()
{
   char a[8]; // allocates 8 sizeof(char) of memory, and sets 'a' to point to its beginning
   printf("Give a string:\n");
   gets(a);
   puts(a);
   // reverses between a and a+4, so reverses first five characters of string beginning at a.
   // In c, an array is actually a pointer to its first element
   func(a,a+4);
   puts(a);
   return 0;
}


with VENICE:

Code: Select all

V   E   N   I   C   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5

p1 = a, p2 = a+4

C   E   N   I   V   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5

p1 = a+1, p2 = a+3

C   I   N   E   V   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5

p1 = a+2, p2 = a+2

C   I   N   E   V   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5
// a+2 swapped with itself


Hence the result is CINEVE, not CENIVE

Lessons: use meaningful identifier names.

User avatar
skeptical scientist
closed-minded spiritualist
Posts: 6142
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: San Francisco

Re: print a string modified-what's wrong?

Postby skeptical scientist » Sun Jul 07, 2013 2:52 am UTC

beojan wrote:with VENICE:

Code: Select all

V   E   N   I   C   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5

p1 = a, p2 = a+4

C   E   N   I   V   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5

p1 = a+1, p2 = a+3

C   I   N   E   V   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5

p1 = a+2, p2 = a+2

C   I   N   E   V   E
^   ^   ^   ^   ^   ^   
a  a+1 a+2 a+3 a+4 a+5
// a+2 swapped with itself


Hence the result is CINEVE, not CENIVE

Lessons: use meaningful identifier names.

[nitpicky]
It doesn't actually swap the N at address a+2 with itself, because when p1=p2=a+2, the p1<p2 check fails and the while loop ends. That's what would happen if the loop was while (p1 <= p2).

You should have noticed that the resulting character was N, not И.
[/nitpicky]
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson

lalop
Posts: 210
Joined: Mon May 23, 2011 5:29 pm UTC

Re: print a string modified-what's wrong?

Postby lalop » Wed Jul 24, 2013 5:56 am UTC

evinda wrote:So,at the command *p1=*p2,the address p1 stays unchanged...Or am I wrong???


That's right, since * refers to the "value at the address of". not the address itself.

Even if you didn't know that for sure, this kind of pattern:

Code: Select all

         ch=*p1;
         *p1=*p2;
         *p2=ch;


should make you think "something is probably being swapped". In addition, the something being swapped seems to be of type char, so that's a pretty big hint that we're swapping the chars at the two pointers.

Finally:

Code: Select all

s = raw_input("Enter string for which to reverse the first 5 characters")
print s[:5][::-1] + s[5:]


Thank you, python!


Return to “Computer Science”

Who is online

Users browsing this forum: No registered users and 4 guests