Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
C++
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char text[100], cuv1[50], cuv2[50], text2[100];
    char sep[] = " '\",.!?";
    int i=0, j=0;
    fgets(text, 100, stdin);
    text[strlen(text) - 1] = '\0';
    fgets(cuv1, 50, stdin);
    cuv1[strlen(cuv1) - 1] = '\0';
    fgets(cuv2, 50, stdin);
    cuv2[strlen(cuv2) - 1] = '\0';
    while(strstr( (text + i) , cuv1))
    {
        int a = strstr( (text + i) , cuv1) - (text + i);
        int b = strstr( (text + i) , cuv1) - (text + i) + strlen(cuv1);
        if( ( (a == 0) || strchr(sep, (text + i)[a - 1]) ) && ( ( (text + i)[b] == '\0') || strchr(sep, (text + i)[b]) ) )
        {
            int x;
            for(x=0; x<a;>{
                text2[j] = (text + i)[x];
                j++;
            }
            i = i + b;
            strcat(text2, cuv2);
            printf("Textul 2 devine: %s\n", text2);
            i = i + strlen(cuv1);
            j = j + strlen(cuv2);
            			
        }
        else
        {
            int x;
            for(x=0; x<b;>{
                text2[j] = (text + i)[x];
                j++;
            }
            i = i + b;
        }
    }
    printf("Textul final este: %s\n", text2);
    return 0;
}
Posted
Updated 11-Dec-15 2:29am
v3
Comments
OriginalGriff 11-Dec-15 8:24am    
And?
What is your problem?
What does the code do that you didn't expect, or not do that you did?
What help do you need?
Jochen Arndt 11-Dec-15 8:36am    
I tried to format your code to be more readable.

But the for loop expressions were already not pasted correctly in your initial post.
So you may use the green 'Improve question' link to fix that (I guess there should be 'x++' instead of '>').

However, it is hard to walk through such undocumented code. Placing some comments helps not only others to understand what you want to achieve.

You should also tell us what / where it does not work as expected. An example would be also helpful (input strings and result). Then problems may be encountered without compiling your code (only a few people here will invest their time to do so).

To summarize the above: Help us to help you.

Here one possible solution in old fashion.

C#
/* NOTE: 
You must control de strOut length if not ... 
or you are in trouble 
the allocate space must be there
*/
int
replace( char *strIn, char *sStr, char *rStr, char *strOut)
{
  /* 
   Validate input fields
  PRECONDITION( strIn !=NULL );
  PRECONDITION( strOut!=NULL );
  PRECONDITION( sStr  !=NULL );
  PRECONDITION( rStr  !=NULL );*/
  
  int i=0,j=0;
  int iLen = strlen( strIn);
  int sLen = strlen( sStr );
  int rLen = strlen( rStr );
  
  //Trim trailing and heading spaces (optional)
  //RTrim(sStr,sLen);
  //LTrim(sStr,sLen);
  
  for( i=0; i<iLen; i++) {
    if( strncmp(&strIn[i], sStr, sLen)) strOut[j++]=strIn[i];
	else {
	  memcpy( &strOut[j], rStr, rLen);
	  j+=rLen;
          i+=(sLen-1);
	}
  }
  strOut[j]=0; 
  return EXIT_SUCCESS;
}

int
main(int argc, char *argv[])
{
  char in[]="jjjadhjhas jhasdjhdakdjah jhasdjhas";
  char sStr[]="as";
  char rStr[]="xxxx";
  char out[200];
  int retCode = replace( in, sStr, rStr, out);
  fprintf(stderr, "Input String  : %s\n", in);
  fprintf(stderr, "Search String : %s\n", sStr);
  fprintf(stderr, "Replace String: %s\n", rStr);
  fprintf(stderr, "Result String : %s\n", out);
  fprintf(stderr, "Return Code: %d\n", retCode);  
  exit(EXIT_SUCCESS);
}
 
Share this answer
 
v6
Like that:

C++
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

void word_replace(char* str, char* word_old, char* word_new)
{
	char* wd_buf = NULL;
	if ((wd_buf = strstr(str, word_old)) != NULL)
	{
		int old_len = strlen(word_old);
		int new_len = strlen(word_new);
		int len = abs(old_len - new_len);
		while (--len >= 0 && old_len < new_len)
		{
			int pos = strlen(str);
			while (--pos >= wd_buf - str)
				str[pos + 1] = str[pos];
		}

		memcpy((void*)&str[wd_buf - str], (const void*)word_new, new_len);

		if (new_len < old_len)
		{
			memcpy((void*)&str[wd_buf - str + new_len],
				(const void*)&str[wd_buf - str + old_len],
                                 strlen(&str[wd_buf - str + old_len]));
		}
	}
}

int main(int argc, char* argv[])
{
	char str[256] = "lazy dog jumps over a brown fox\0";

	char word_old[256] = "dog";
	char word_new[256] = "straus";

	word_replace(str, word_old, word_new);

	printf("%s\n", str);

	_getch();

	return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900