There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
black screen blue screen boot computer connection crash css dell display driver drivers error excel firefox firefox 3 game hard drive internet internet explorer itunes laptop lcd linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition password printer problem problems ram router slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
To check if a sequence is repeated in a string(in C)


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
chatterjee's Avatar
Computer Specs
Senior Member with 578 posts.
 
Join Date: Jul 2005
Location: India,Howrah
Experience: A teen with a keen tech mind(Intermediate)
15-Aug-2006, 12:52 PM #1
Question To check if a sequence is repeated in a string(in C)
Dear fellows I've got stuck with a problem.

I want to write a program to check if a sequence of words is immediately repeated or not.

Suppose you entered a string-'bubble' ,it will be rejected because it has b and b subsequently.
similarly 'xyxy210' or 'abcabc154' will be rejected whereas 'abcbca154' will be accepted.

I thought over it again and again but found no result.

Any kind of help will be greatly appreciated.

Thanks in advance.
__________________
“Take up one idea. Make that one idea your life - think of it, dream of it, live on idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone. This is the way to success.”


.......Swami Vivekananda
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
15-Aug-2006, 01:51 PM #2
Hint: The solution to this problem is recursive.

The "end case" is where the length of the string <= 2.
kidnewbie's Avatar
Member with 84 posts.
 
Join Date: Mar 2006
16-Aug-2006, 07:24 PM #3
Quote:
Originally Posted by chatterjee
Dear fellows I've got stuck with a problem.

I want to write a program to check if a sequence of words is immediately repeated or not.

Suppose you entered a string-'bubble' ,it will be rejected because it has b and b subsequently.
similarly 'xyxy210' or 'abcabc154' will be rejected whereas 'abcbca154' will be accepted.

I thought over it again and again but found no result.

Any kind of help will be greatly appreciated.

Thanks in advance.
'abcbca154' should also be rejected since 'bc' is repeated

i could have helped you if this question was asked 3 months ago. my programming is kinda rusty cause i haven't had any practice.

i'll still try look into it and post anything that might be helpful

but like AGCurry said, recursion is definitely the answer
Jimmy the Hand's Avatar
Senior Member with 794 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: With Excel, fairly good.
17-Aug-2006, 01:45 AM #4
I don't use C, but here's an algorhythm for you.

Given is a string, which is to be examined. Call it SStr. For example, SStr = "asehsehateafr"

1. Pick the 1st character in SStr. In this example, it will be "a".
2. Look for the next "a" in the string. In this example, it will be at character position 8.
3. Copy two substrings, one starting with the 1st "a" the other starting with the 2nd "a", and both of them 7 characters long (distance between the two "a"-s). May want to put the substrings into variables.
4. Compare the substrings. If they are the same, take appropriate action. If not, step forward in algorhythm.
5. Look for the next "a" in SStr. This, if found, will be the 3rd "a" in the string. Make substrings, compare them. Etc.
6. When there are no more "a"-s, or the distance between the first "a" and the currently found "a" is more than half of the string length, exit cycle.
7. Pick the 2nd character in SStr. In this example, it will be "s".
8. Process it as you did with "a"
9. Do the same with each character of SStr, until you reach the last one ("r").
__________________
'
It is advised to provide a clear, detailed description of the task, so that others can understand it, and offer the best possible help. Otherwise, you risk experts ignoring your request.
chatterjee's Avatar
Computer Specs
Senior Member with 578 posts.
 
Join Date: Jul 2005
Location: India,Howrah
Experience: A teen with a keen tech mind(Intermediate)
18-Aug-2006, 11:09 PM #5
Thank you all for giving your helping hand.

To tell the truth,I'm a C newbie coz I've learnt the language a month or two ago.And that's why I'm not that comfortable with recursion still now.

Malcka,I'll surely try your suggestion.But there is a problem...in BASIC I could have used the MID$ function to cut the string at the desired position but I don't know which function I should use to do the same in C.

BTW,thank you for your support.Please provide me with any ideas thant come come into your mind about solving this problem.

And lastly,I'm extremely sorry for giving you a wrong example.Rather,this should be the example'abcdcbad54'
__________________
“Take up one idea. Make that one idea your life - think of it, dream of it, live on idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone. This is the way to success.”


.......Swami Vivekananda
blaqDeaph's Avatar
Senior Member with 869 posts.
 
Join Date: Nov 2005
Location: Down Under!
Experience: Enough
19-Aug-2006, 02:27 AM #6
Wouldnt it be easier to use for loops and strstr()? May be more elegant with recursion though.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:27 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.