C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 5th, 2013, 02:01 AM
swapy swapy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 66 swapy Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 4 m
Reputation Power: 0
Boyre moore string comparison

hello friends ,
I am trying to implement boyre moore algorithm for string comparison..

Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>
//boyre moore string algorithm
/*  given: ABXBABC
	find:  ABC
*/

void boyre(char *str1,char *str2,int maxpos1,int maxpos2)
	{
		char ch[10];
		str1=malloc(10*sizeof(char));
	 	str2=malloc(10*sizeof(char));
	 //	ch=malloc(10*sizeof(char));
	 	
	 	while(maxpos2>=0)
	 		if(str1[maxpos2]==str2[maxpos2])
			{
				//compare prev element
				ch[maxpos2]=str1[maxpos2];
				maxpos2--;
				boyre(str1,str2,maxpos1,maxpos2);
			}
			else
			{
				maxpos2=maxpos2+maxpos2+1;
				boyre(str1,str2,maxpos1,maxpos2);
			}
		if(maxpos2==maxpos1)
			printf("\n no match found");

	        puts(ch);
		}

int main()
	 {
	 	char *str1;
	 	char *str2;
	 	int len1,len2;
	 	int maxpos1,maxpos2;
	 //	int i=0;
	 	
	 	str1=malloc(10*sizeof(char));
	 	str2=malloc(10*sizeof(char));
	 	
	 	printf("Enter string you want to compare with :\n");
	 	gets(str1);
	 	printf("Enter comparison string :\n");
	 	gets(str2);
	 	
	 	len1=strlen(str1);
	 	len2=strlen(str2);
		
		printf("%d",len1);
		printf("%d",len2);
		
		maxpos1=len1-1;
		maxpos2=len2-1; 
		
		boyre(str1,str2,maxpos1,maxpos2);
	


	 getch();
	 return 0;
	 }


What i have tried so far:
string:ABCXABXC
comparewith:ABC

started from right of comparewith string ie C if match found then i decrement my position and check for B and so on .. if not I jump n positions ahead ..
where n=string length of comparewith string.....

I have problems in code....

I have understood algorithm only this far... any additons to check?

Last edited by swapy : February 5th, 2013 at 02:09 AM.

Reply With Quote
  #2  
Old February 5th, 2013, 03:05 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
Code:
void boyre(char *str1,char *str2,int maxpos1,int maxpos2)
	{
		char ch[10];
		str1=malloc(10*sizeof(char));
	 	str2=malloc(10*sizeof(char));

Let's pretend that you know to never use gets because of the inherent buffer overrun problem. You've passed information to your boyre function then immediately lose track of it by allocating new space. Frankly, I expected quite a different implementation. The algorithm is fast when you precompute a table of search alignment jumps for characters matched in certain positions. To me a recursive approach looks conceptually deficient.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old February 6th, 2013, 01:58 AM
swapy swapy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 66 swapy Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 4 m
Reputation Power: 0
i didnt understand what you mean..
ok should i pass str1[10] str2[10] instead of malloc?

Reply With Quote
  #4  
Old February 6th, 2013, 11:29 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
Read this program closely, predict the output, run the program. Does it meet your expectation? How is it similar to the program in your post?
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {
  char*s,*t;
  int n;
  puts("PROGRAM TO ILLUSTRATE INFORMATION LOSS IN YOURS");
  puts("YOU'D EXPECTED THE SAME MESSAGE TO PRINT TWICE.");
  t = s = malloc(999);
  if (NULL == s) exit(0);
  strcpy(s,"Information I want!");
  n = strlen(s);
  puts("DISPLAYING s");
  puts(s);
  s = malloc(999);
  if (NULL == s) { free(t); exit(0); }
  s[n] = 0;
  puts("DISPLAYING s AGAIN");
  puts(s);
  free(t);
  free(s);
  puts("DONE");
  return 0;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Boyre moore string comparison

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap