Thread: problem with pointers and strings (replace pattern program)

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Exclamation problem with pointers and strings (replace pattern program)

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using a global variable in your function? It makes it annoyingly hard to follow when you have to look all over to see what a given variable actually is. Rethink the process:
    Code:
    count the occurances of the word to replace
    newstring = strlen oldstring - word to replace * occurances + strlen newword * occurances + 1
    allocate space for newstring
    while not at the end of the big string
        while not at the first letter of the word to replace
            copy characters into new string
        copy in one occurance of new word
        increment past word to replace in old string
    Something along those lines.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    thanks

    but you dont help a lot

    i tried but no results

    some source code will help a lot

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>some source code will help a lot
    Then try writing some, and post it here when it doesn't do what you want.

    Here's a couple of pointers
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > same_str=mem_alloc(buffer_str);
    > same_str=buffer_str;

    You do realize you just created a memory leak, right? You allocated some memory for same_str, then pointed it back to buffer_str, thereby losing the original memory.

    > repl_str=mem_alloc(buffer_str);
    > repl_str=buffer_str;

    Oops, you just did it again. Try strcpy() instead:
    same_str=mem_alloc(buffer_str);
    strcpy( same_str, buffer_str );

    You should also free same_str and repl_str once you're finished using them:
    free(same_str);
    free(repl_str);

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void replace ( char *string, char *find ) {
        char *p = strstr( string, find );
        if ( p != NULL ) {
            printf( "Left  Substring='%.*s'\n", p-string, string );
            printf( "Match Substring='%s'\n", find );
            printf( "Right Substring='%s'\n", p+strlen(find) );
        }
    }
    
    int
    main(void)
    {
        replace( "can you find the floogle", "find" );
        return 0;
    }
    I suggest you write a function which makes ONE substitution in a string, and returns a boolean indicating whether a substitution was made.

    Then write another function to call it either
    - once (firstOnly = true)
    - until it returns false (firstOnly = false)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM
  5. need more help with pointers and strings
    By bgbfflochp in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2002, 08:31 AM