473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

C puzzle


Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)

Jan 2 '07 #1
16 3056
deepak said:
>
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?
Surely that's your job?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 2 '07 #2
Hello,
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)
This is not really complicated. A possible algorithm consists to:

1) Find n, such that s2[n] == s1[0]
2) Build a temporary string y which is s2 rotated of n positions.
3) Compare s1 and y.

Cheers,
Loic.

Jan 2 '07 #3

lo******@gmx.net wrote:
Hello,
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)

This is not really complicated. A possible algorithm consists to:

1) Find n, such that s2[n] == s1[0]
2) Build a temporary string y which is s2 rotated of n positions.
3) Compare s1 and y.
This may not work if
s1 = ABCADE and s2 = CADEAB
s2[n] = A, where n = 1. temporary string y = ADEABC.
s1 compares not equal to y.
I mean to say your algorithm will fail, if there is more than one s1[0]
in string s2.

Jan 2 '07 #4

deepak wrote:
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)
/* parody of computer science homework
given
a[0]...a[n-1]
b[0]...b[n-1]
evaluate
n==0 || exist 0<=k<n (forall 0<=i<n (a[i] == b[(i+k) mod n]) )
*/

#include <string.h>
#include <assert.h>

void
assert_invariant( const char *a, const char *b, size_t n, size_t k,
size_t i, size_t j )
{
size_t t = 0;

assert( j == (i+k)%n );
for ( t=0; t < i; t++ )
assert( a[t] == b[(t+k)%n] );

}

/* return true iff forall 0<=i<n (a[i] == b[(i+k)%n]) */
int
is_rot_at_offset( const char *a, const char *b, size_t n, size_t k )
{
size_t i =0, j = 0;

assert( 0 <= k && k < n ); /*precondition*/
j = i+k;

assert_invariant( a, b, n, k, i, j );

while ( i<n && a[i] == b[j] )
{
j++;
if ( j >= n )
j = 0;
i++;
assert_invariant( a, b, n, k, i, j );
}
assert_invariant( a, b, n, k, i, j );
assert( 0 <= i && i <= n );
assert( i==n || a[i] != b[j] );
return i == n;
}

/* return true iff n == 0 || exist 0<=k<n (forall 0<=i<n (a[i] ==
b[(i+k)%n]) )*/
int
is_rot( const char *a, const char *b )
{
size_t k =0;
size_t n =0;

if ( a == NULL )
return b == NULL;
assert( a != NULL );
if ( b == NULL )
return 0;
assert( a != NULL );
assert( b != NULL );

n = strlen(a);
if ( n != strlen(b) )
return 0;
assert( n == strlen(a) && n == strlen(b) );

while ( k < n && ! is_rot_at_offset( a, b, n, k ) )
k++;
return n == 0 || k != n;
}

void
test_is_rot()
{
assert( is_rot(NULL,NULL) );
assert( !is_rot(NULL,"") );
assert( !is_rot("",NULL) );
assert( is_rot("","") );
assert( !is_rot("","a") );
assert( !is_rot("a","") );
assert( is_rot("a","a") );
assert( is_rot("ab","ab") );
assert( is_rot("ba","ab") );
assert( is_rot("ab","ba") );
assert( !is_rot("abc","ba") );
assert( !is_rot("ba","abc") );
assert( is_rot("abc","cab") );
assert( is_rot("abc","bca") );
assert( !is_rot("ba","abc") );
assert( is_rot("abcd","cdab") );
assert( is_rot("cdab","abcd") );
}
int
main()
{
test_is_rot();

return 0;
}

Jan 2 '07 #5
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?
>
(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)
This is not really complicated. A possible algorithm consists to:

1) Find n, such that s2[n] == s1[0]
2) Build a temporary string y which is s2 rotated of n positions.
3) Compare s1 and y.
This may not work if
s1 = ABCADE and s2 = CADEAB
s2[n] = A, where n = 1. temporary string y = ADEABC.
s1 compares not equal to y.
I mean to say your algorithm will fail, if there is more than one s1[0]
in string s2.
Absolutely true. But do you really want me to remove all the fun of
this homework? ;-)

Cheers,
Loic.

Jan 2 '07 #6
deepak <de*********@gmail.comwrote:
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?
It's a snap - write your own strstr routine and then you can call it
as many times as you want...

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jan 2 '07 #7

lo******@gmx.net wrote:
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)
>
This is not really complicated. A possible algorithm consists to:
>
1) Find n, such that s2[n] == s1[0]
2) Build a temporary string y which is s2 rotated of n positions.
3) Compare s1 and y.
>
This may not work if
s1 = ABCADE and s2 = CADEAB
s2[n] = A, where n = 1. temporary string y = ADEABC.
s1 compares not equal to y.
I mean to say your algorithm will fail, if there is more than one s1[0]
in string s2.

Absolutely true. But do you really want me to remove all the fun of
this homework? ;-)
I think you probably intended to repeat those three steps in a loop
until the end of s2 is reached.

Jan 2 '07 #8

lo******@gmx.net wrote:
Given a string s1 and a string s2, write a snippet to say whether s2
is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)
>
This is not really complicated. A possible algorithm consists to:
>
1) Find n, such that s2[n] == s1[0]
2) Build a temporary string y which is s2 rotated of n positions.
3) Compare s1 and y.
>
This may not work if
s1 = ABCADE and s2 = CADEAB
s2[n] = A, where n = 1. temporary string y = ADEABC.
s1 compares not equal to y.
I mean to say your algorithm will fail, if there is more than one s1[0]
in string s2.

Absolutely true. But do you really want me to remove all the fun of
this homework? ;-)
I think you probably intended to repeat those three steps in a loop
until the end of s2 is reached.

Jan 2 '07 #9
"ju**********@yahoo.co.in" wrote:
lo******@gmx.net wrote:
>>
>>Given a string s1 and a string s2, write a snippet to say whether
s2 is a rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)

This is not really complicated. A possible algorithm consists to:

1) Find n, such that s2[n] == s1[0]
2) Build a temporary string y which is s2 rotated of n positions.
3) Compare s1 and y.
This may not work if
s1 = ABCADE and s2 = CADEAB
s2[n] = A, where n = 1. temporary string y = ADEABC.
s1 compares not equal to y.
I mean to say your algorithm will fail, if there is more than one s1[0]
in string s2.
Much simpler will work. No calls to strstr are needed. If the
mood hits I will publish a solution later, after the OP's date for
passing in his homework has been passed. Although the solution
using a single strstr call is fairly elegant and quite efficient.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 2 '07 #10
Hi,
Much simpler will work. No calls to strstr are needed. If the
mood hits I will publish a solution later, after the OP's date for
passing in his homework has been passed. Although the solution
using a single strstr call is fairly elegant and quite efficient.
I agree that the solution of /strstr()/ is fairly elegant.

Cheers,
Loic.

Jan 2 '07 #11

lo******@gmx.net skrev:
Hi,
Much simpler will work. No calls to strstr are needed. If the
mood hits I will publish a solution later, after the OP's date for
passing in his homework has been passed. Although the solution
using a single strstr call is fairly elegant and quite efficient.

I agree that the solution of /strstr()/ is fairly elegant.

Cheers,
Loic.
Append str1 to itself and do a strstr for str2

Jan 2 '07 #12
On Tue, 2 Jan 2007 10:58:42 -0600, dspfun wrote
(in article <11**********************@h40g2000cwb.googlegroups .com>):
>
lo******@gmx.net skrev:
>Hi,
>>Much simpler will work. No calls to strstr are needed. If the
mood hits I will publish a solution later, after the OP's date for
passing in his homework has been passed. Although the solution
using a single strstr call is fairly elegant and quite efficient.

I agree that the solution of /strstr()/ is fairly elegant.

Cheers,
Loic.

Append str1 to itself and do a strstr for str2
*sigh*

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Jan 2 '07 #13
Good evening,
Much simpler will work. No calls to strstr are needed. If the
mood hits I will publish a solution later, after the OP's date for
passing in his homework has been passed. Although the solution
using a single strstr call is fairly elegant and quite efficient.
I agree that the solution of /strstr()/ is fairly elegant.

Cheers,
Loic.

Append str1 to itself and do a strstr for str2
close, but not yet 100% correct.

Cheers,
Loic.

Jan 2 '07 #14

lo******@gmx.net wrote:
Good evening,
Much simpler will work. No calls to strstr are needed. If the
mood hits I will publish a solution later, after the OP's date for
passing in his homework has been passed. Although the solution
using a single strstr call is fairly elegant and quite efficient.
>
I agree that the solution of /strstr()/ is fairly elegant.
>
Cheers,
Loic.
Append str1 to itself and do a strstr for str2

close, but not yet 100% correct.
Do you mean to say that the size of s1 and s2 should also be checked ?
str1 = AAA
str2 = AAAA
append str1 to itself to get str3, str3 = AAAAAA
strstr() for str2 in str3 will be a success.

Or is there any other corner case that you have in your mind ?

Jan 3 '07 #15
Randy Howard said:
On Tue, 2 Jan 2007 10:58:42 -0600, dspfun wrote:
>>
Append str1 to itself and do a strstr for str2

*sigh*
Whilst it's true that two O(n) operations still give you O(n), that doesn't
make it any less painful to watch, does it?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 3 '07 #16
Hello,
Append str1 to itself and do a strstr for str2
close, but not yet 100% correct.

Do you mean to say that the size of s1 and s2 should also be checked ?
yes, exactly.
Or is there any other corner case that you have in your mind ?
The proof that there is no other corner case if left as an exercice to
the reader.

Cheers,
Loic.

Jan 3 '07 #17

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

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.