help with strings

This is a discussion on help with strings within the C Programming forums, part of the General Programming Boards category; I'm having trouble with the function 'replace' in my program. The program is supposed to work where the user enters ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    help with strings

    I'm having trouble with the function 'replace' in my program. The program is supposed to work where the user enters a string of 2 or more words. It first checks to how many times the 1st word is repeated and prints the number. Then it is supposed to replace the 1st word each time it occurs in the string with the second word and print the new string. The function 'replace' is supposed to do the job of replacing the 1st word with the 2nd throughout the string. For some reason, it's not saving anything in my string2.

    here's the source...
    Code:
    #include <stdio.h>
    #include <string.h>
    #define N 100
    
    void getfirst(const char *string, char *one);
    void getsecond(const char *string, char *two);
    int stringcount(const char *string, const char *one);
    void replace(const char *string, const char *one, const char *two, char *string2);
    
    main()
    
    
      {
    	char string[N+1]={'\0'}, word1[N+1]={'\0'}, word2[N+1]={'\0'};
    	char string2[N+1]={'\0'};
    	int number;
    	printf("Input a line with at least two words:\n");
    	gets(string);
    	getfirst(string, word1);
    	getsecond(string, word2);
    	number = stringcount(string, word1);
    	printf("The number of occurrences of the first word %s is %d", word1, number);
    	replace(string, word1, word2, string2);
    	puts(string2);
    	
    	return 0;
      }
    
    void getfirst(const char *string, char *one)
      {
    	int i;
    	
    	for(i=0; string[i] != ' '; i++)
    	  {
    		one[i] = string[i];
    	  }
    	
      }
    
    void getsecond(const char *string, char *two)
      {
    	int i, j;
    	
    	for(i=0; string[i] != ' '; i++)
    		{}
    	  for(j=0; string[i]!=' '; j++, i++)
    	  {
    		two[j] = string[i];
    	  }
      }
    
    int stringcount(const char *string, const char *one)
      {
    	
    	{int i,n,m;
    		int count = 0;
    		for(i=0; i<N; i++)
    			if(one[0] == string[i]){
    				for(n=0, m=i; one[n]!='\0' ; n++, m++)
    				{
    					if( one[n] == string[m])
    						{if(one[n+1]=='\0')
    							count++;}
    					else if(one[n] != string[m])
    						continue;
    				}
    			}
    		return count;
    	}
      }
    
    void replace(const char *string, const char *one, const char *two, char *string2)
      {
    		{int i,n,m,j,k,q=0;
    		for(i=0; i<N; i++)
    			if(one[0] == string[i]){
    				for(n=0, m=i; one[n]!='\0' ; n++, m++)
    				{
    					if( one[n] == string[m])
    						{if(one[n+1]=='\0')
    							{for(j=0; j<N; j++, q++)
    								string2[q]=two[j];}	
    						}
    					else if(one[n] != string[m])
    						string2[q]=string[i];q++;
    				}}
    			else if(one[0]!=string[i])
    				string2[q] = string[i];
    				q++;
    			}
      }

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Your function getfirst doesn't return any value???
    Code:
    void getfirst(const char *string, char *one)
      {
    	int i;
    	
    	for(i=0; string[i] != ' '; i++)
    	  {
    		one[i] = string[i];
    	  }
    Wouldn't you want the array i stored for the first word like
    Code:
    char getfirst(const char *string, char *one){
                 	int i;
    	for(i=0; string[i] != ' '; i++)
    	  {
    		one[i] = string[i];
    	  }
    	  return i;
    }
    then have it assigned to the main function and then use it for getsecond and replace. replace doesn't have the i you want. I think?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Your getsecond() function isn't returning anything. It should look more like this:
    Code:
    void getsecond(const char *string, char *two)
      {
    	int i, j;
    	
    	for(i=0; string[i] != ' '; i++)
    		{}
    
    	for (;string[i] == ' '; i++);
    
    	  for(j=0; string[i]!=' '; j++, i++)
    	  {
    		two[j] = string[i];
    	  }
    	two[j] = '\0';
      }
    Put some printf() statements in your main() to see what each function returns.

    Your replace() function needs work also. You're missing some brackets for one thing, but in addition the logic isn't quite right. Again use printf()'s to see what is happening.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Hi,
    Questions seems to be weird (said search and replace a string but considering only two words??). put some example.
    Saravanan.T.S.
    Beginner.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    I have my code working how it should except when the first and second word begin the same. For example,

    if the input is> abc def abcdef

    the final output should be> def def abcdef

    What it's doing is giving > def def def

    any suggestions?

    Code:
    #include <stdio.h>
    #include <string.h>
    #define N 100
    
    int stringcount(const char *string, const char *one);					
    void replace(const char *string, const char *one, const char *two, char *string2, int comp);
    int compare(const char *one, const char *two);
    
    main()
    
    
      {
    	char string[N+1]={'\0'}, stringwhole[N+1]={'\0'}, word1[N+1]={'\0'}, word2[N+1]={'\0'};	
    	char string2[N+1]={'\0'};						
    	int number, i, comp;			
    	printf("Input a line with at least two words:\n");	
    	scanf("%s", word1);				
    	scanf("%s", word2);					
    	gets(string);							
    	strcat(stringwhole, word1);
    	for(i=0; stringwhole[i]!='\0'; i++)
    		{}
    	stringwhole[i]=' ';
    	strcat(stringwhole, word2);
    	strcat(stringwhole, string);
    	number = stringcount(stringwhole, word1);		
    	comp = compare(word1, word2);				
    	printf("The number of occurrences of the first word %s is %d\n", word1, number);
    	replace(stringwhole, word1, word2, string2, comp);	
    	puts(string2);						
    	
    
    	return 0;
      }
    
    
    
    int stringcount(const char *string, const char *one)
      {
    	
    	{int i,n,m;							
    		int count = 0;						
    		for(i=0; i<N; i++)					
    			if(one[0] == string[i]){			
    				for(n=0, m=i; one[n]!='\0' ; n++, m++)
    				{
    					if( one[n] == string[m])
    						{if(string[m+1]=='\0' || string[m+1]==' ')
    							count++;}
    					else if(one[n] != string[m])
    						continue;
    				}
    			}
    		return count;
    	}
      }
    
    void replace(const char *string, const char *one, const char *two, char *string2, int comp)
      {	
      int i,j,n=0,m,q=0;
    	
      for(i=0; i<N; i++)							
    	{						
    		if(string[i] == one[0])
    			{						
    				for(n=0, m=i; one[n]!='\0'; n++, m++)	
    					{					
    						if(one[n] == string[m])		
    						{
    							if(string[m+1]==' ' || string[m+1]=='\0')
    							{
    								for(j=0; two[j]!='\0'; j++)
    									{
    										string2[q]=two[j];
    										q++;
    									} 
    							}
    						}
    						
    					}
    					
    				if(two[j]=='\0')
    					{
    						if(comp>=0)
    							{
    								i+=m;
    							}
    						if(comp<0)
    							{
    								i=m;
    							}
    					}
    						
    			}
    		string2[q] = string[i];
    		q++;
    			
    	}	
      }
    
    int compare(const char *one, const char *two)
      {
    	int i, j;				
    	for(i=0; one[i]!='\0'; i++)		
    		{}				
    	for(j=0; two[j]!='\0'; j++)		
    		{}
    	if(i<j)
    		return 1;
    	if(i=j)
    		return 0;
    	if(i>j)
    		return -1;
      }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    void replace( const char *string, const char *one, const char *two, char *string2 )
    {
       int i, j, k, n;
    
       for ( i=0,n=0; string[i] != '\0'; )
       {
          while ( string[i] != '\0' && isspace(string[i]) )
             string2[n++] = string[i++];
          if (string[i] == one[0])
          {
             for ( j=i+1,k=1; string[j] != '\0' && one[k] != '\0' && string[j] == one[k]; j++,k++ );
             if ( one[k] == '\0' && (ispunct(string[j]) || isspace(string[j])) )
             {
                i = j;
                // Replace
                for ( k=0; two[k] != '\0'; k++ )
                   string2[n++] = two[k];
             }
             else
             {
                while ( string[i] != '\0' && !isspace( string[i] ) )
                   string2[n++] = string[i++];
             }
          }
          else
          {
             while ( string[i] != '\0' && !isspace( string[i] ) )
                string2[n++] = string[i++];
          }
       }
       string2[n] = '\0';
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21