Current location: Hot Scripts Forums » Programming Languages » C/C++ » replace pattern-problm with pointers


replace pattern-problm with pointers

Reply
  #1 (permalink)  
Old 05-25-04, 02:30 AM
Erev0s Erev0s is offline
New Member
 
Join Date: May 2004
Location: Hirakleio
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy replace pattern-problm with pointers

Hi guys i need your help for this replace_pattern program.

This is a program that takes a string
i.e.:"The boy was sitting in the room"
then it takes a string to compare
i.e.:"the"
Finally it takes a string to replace the second string
i.e:"a"

The final result is :"a boy was sitting in a room"

I must use this function:
char *replace_pattern(char *s, char *p ,char *np,int firstOnly);

Here is my program ...(i cant find what is wrong)
The bug is in replace_pattern function


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

char *replace_pattern(char *s, char *p ,char *np,int firstOnly);

//this funct allocates dynamicly memory for the three strings
//s,p and np
char *mem_alloc(char *buffer);

char buffer_str[81];
char *cur_str,*same_str,*repl_str,*new_str;

int firstOnly,c;

int main(void){
   
   char *str_ptr;
  
   printf("Enter a string and then press enter...\n");
 
   while( *(str_ptr=gets(buffer_str)) == NULL ) 
   printf("try again...\n");

   cur_str=mem_alloc(buffer_str);
   cur_str=buffer_str;

 printf("Enter a string to compare,then press enter...\n"); 
 while( *(str_ptr=gets(buffer_str)) == NULL )
   
   same_str=mem_alloc(buffer_str);
   same_str=buffer_str;
   
  printf("Enter a string to replace,then press enter...\n");  
 while( *(str_ptr=gets(buffer_str)) == NULL )
 printf("try again...\n");
 
   repl_str=mem_alloc(buffer_str);
   repl_str=buffer_str;
   
   printf("\nEnter 0 if you want to check the whole string...");
   printf("\nor enter a different number if you want to find");
   printf("\nand replace the 1st similarity:");
   c=scanf("%d",&firstOnly);
   
   while(c!=1){
	while((c=getchar())!='\n');
	c=scanf("%d",&firstOnly);
	}
   
   new_str=replace_pattern(cur_str, same_str ,repl_str, firstOnly);
 return 0;
}
/*memory allocation for the strings*/
Code:
char *mem_alloc(char *buffer)
{
 char *str;
 
	str=(char *)malloc((strlen(buffer)+1)); 
	if(str==NULL){
	printf("\n memory allocation error! ");
  printf("\n exiting now... ");
  exit(1);
  }
  else return(str);  
}
Replace Pattern function *buggy one*
/* *s is the given string, *p is a string to search in *s,
if we find *p string in *s string we replace with *np string

if firstOnly !=0 i must replace only the first substring with np
else i must replace substrings with np string in the whole string*/
Code:
char *replace_pattern(char *s, char *p ,char *np)
{
char *start_ptr;
int counter=0,i=0,j=0;
 
 counter=strlen(p);
 while((start_ptr=strstr(s,p))!=NULL){
   
   while(s<start_ptr)
   
   buffer_str[i++]=*s++;
	while(j<counter)
   buffer_str[i++]=*np++;
   s+=counter;
   }
   buffer_str[i++]='\0'; 
 return buffer_str;
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 01:26 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.