469,352 Members | 1,661 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

I want to write a code which replace words from string without use of string function

pntkiran
HI All.

I want to write a code which replace words from string.without use of string function

Example str = "This is string" remove "is" from string and replace with "was".
so, finally str = "This was string".

Void replace ( char * str , char * pattern1, char * pattern2).

Thanks in advance.

Kin Parmar.
Jun 2 '07 #1
12 27451
Savage
1,764 Expert 1GB
HI All.

I want to write a code which replace words from string.without use of string function

Example str = "This is string" remove "is" from string and replace with "was".
so, finally str = "This was string".

Void replace ( char * str , char * pattern1, char * pattern2).

Thanks in advance.

Kin Parmar.
Ok,what have u done so far?

Please ask about a sepcifice quesution and please read posting guidelines if u haven't already.

Savage
Jun 2 '07 #2
Ok,what have u done so far?

Please ask about a sepcifice quesution and please read posting guidelines if u haven't already.

Savage
I want to find out word from string and replace with new word.

like wise, I have string " Great People here" from that replace "People" and put "Man".

I write simple code here :-
but need help :-

Expand|Select|Wrap|Line Numbers
  1. char* str = "this is string";
  2. char* pattern1 = "is";
  3. char* pattern2 = "was";
  4.  
  5. int slen = strlen(str);
  6.  
  7. for (int i = 0; i <slen; i++)
  8. {
  9.         for (int j =0; j < slen; j++)
  10.         {
  11.                 if (pattern1[i] == str[j] )
  12.                 {                    
  13.                    //str[j]=pattern2[i]; /// Want help here, how to replace string.  
  14.  
  15.                 }
  16.         }
  17.  
  18. }
Jun 2 '07 #3
Savage
1,764 Expert 1GB
I want to find out word from string and replace with new word.

like wise, I have string " Great People here" from that replace "People" and put "Man".

I write simple code here :-
but need help :-

char* str = "this is string";
char* pattern1 = "is";
char* pattern2 = "was";

int slen = strlen(str);

for (int i = 0; i <slen; i++)
{
for (int j =0; j < slen; j++)
{
if (pattern1[i] == str[j] )
{
//str[j]=pattern2[i]; /// Want help here, how to replace string.

}
}

}
In order to do what u want to do,u must first tokenize ur string,uisng a whitespace as a token.
e.g

string="This is a string"

after tokenizingL

string1->"This"
string2->"is"
string3->"a"
string4->"string"

I suppose u don't want to use string function to tokenize?

Savage
Jun 2 '07 #4
AdrianH
1,251 Expert 1GB
I want to find out word from string and replace with new word.

like wise, I have string " Great People here" from that replace "People" and put "Man".

I write simple code here :-
but need help :-

Expand|Select|Wrap|Line Numbers
  1. char* str = "this is string";
  2. char* pattern1 = "is";
  3. char* pattern2 = "was";
  4.  
  5. int slen = strlen(str);
  6.  
  7. for (int i = 0; i <slen; i++)
  8. {
  9.         for (int j =0; j < slen; j++)
  10.         {
  11.                 if (pattern1[i] == str[j] )
  12.                 {                    
  13.                    //str[j]=pattern2[i]; /// Want help here, how to replace string.  
  14.  
  15.                 }
  16.         }
  17.  
  18. }
Without using c code, tell me your algorithm. I think it will make it clearer to both you and me.


Adrian
Jun 2 '07 #5
Without using c code, tell me your algorithm. I think it will make it clearer to both you and me.


Adrian
I want to search word form string and then replace that word with other word.
Jun 2 '07 #6
AdrianH
1,251 Expert 1GB
I want to search word form string and then replace that word with other word.
No, not the end result, the algorithm. What steps do you need to do in order to get the desired end result.

Say you had a peice of paper in front of you with a bunch of boxes and a letter in each box. How would you find the string you want to replace?


Adrian
Jun 2 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
This aint'a gonna work:

Expand|Select|Wrap|Line Numbers
  1. char* str = "this is string";
  2.  
str is a constant. The compiler will never let you change a literal. Use a non-const array instead:

Expand|Select|Wrap|Line Numbers
  1. char str[] = "this is string";
  2.  
Here you have a char array initialized with the string.

Now have fun.
Jun 2 '07 #8
AdrianH
1,251 Expert 1GB
This aint'a gonna work:

Expand|Select|Wrap|Line Numbers
  1. char* str = "this is string";
  2.  
str is a constant. The compiler will never let you change a literal. Use a non-const array instead:

Expand|Select|Wrap|Line Numbers
  1. char str[] = "this is string";
  2.  
Here you have a char array initialized with the string.

Now have fun.
The algorithm is wrong anyway wfc. He/she needs to revisit it before moving on to more mundane things such as causing a SEG FAULT. ;)


Adrian
Jun 2 '07 #9
so...you dont know how to help this guy/gal??

I think (s)he is looking for something like this:
sentence="He who can not be named"
newsentence=replaceword(sentence,"not","always");

so
newsentence="He who can always be named"


How do you accomplish this?

or are you saying this cant be done unless a call to an outside program (like sed) is used?

I am looking for this trick too
I can read a string from a file - but I need to alter it.
strtok doesnt seem to work on the PSP...so I need an alternate method

...and I cant seem to find this answer on google either
C>64
Jun 8 '07 #10
gpraghuram
1,275 Expert 1GB
Hi,
Then you have to write two functions.
1)One funstion similar to strstr funstion (finding a substring)
2)Replace function

Strstr function returns the position of the matching substring and put the replace characters in that place of original substring and concatenae remaning part of the original string

Thanks
Raghuram
Jun 8 '07 #11
AdrianH
1,251 Expert 1GB
so...you dont know how to help this guy/gal??

I think (s)he is looking for something like this:
sentence="He who can not be named"
newsentence=replaceword(sentence,"not","always");

so
newsentence="He who can always be named"


How do you accomplish this?

or are you saying this cant be done unless a call to an outside program (like sed) is used?

I am looking for this trick too
I can read a string from a file - but I need to alter it.
strtok doesnt seem to work on the PSP...so I need an alternate method

...and I cant seem to find this answer on google either
C>64
We can help, we just don't give out the answers. ;)

gpraghuram has the functions right, but you have to come up with the algorithm. You will have to probably reallocate the memory space for the string as the resulting string may be longer then the original one.


Adrian
Jun 8 '07 #12
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. void main()
  5. {
  6. char a[50],b[10],c[10],d[50],t[50];
  7. int n,m,i,o,j,l,k=0,l1=0;
  8. clrscr();
  9. printf("\n\nType the sentence  :");
  10. gets(a);
  11. printf("\n\n\nsub string :");
  12. gets(b);
  13. printf("\n\n\nReplacing  :");
  14. gets(c);
  15. n=strlen(b);
  16. o=strlen(a);
  17. m=strlen(c);
  18. for (i=0;i<o;i++)
  19. {
  20. for(j=0;j<n;j++)
  21. t[j]=a[i+j];
  22. t[j]='\0';
  23. if (strcmp(t,b)==0)
  24. {
  25. k++;
  26. for(l=0;l<m;l++)
  27. {
  28. d[l1]=c[l];
  29. l1++;
  30. }
  31. i=i+(n-1);
  32. }
  33. else
  34. {
  35. d[l1]=a[i];
  36. l1++;
  37. }
  38. }
  39. d[l1]='\0';
  40. printf("\n\n\nThe number of string =%d",k);
  41. printf("\n\n\nAnswer=%s",d);
  42. getch();
  43. }
Oct 23 '13 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Chris Cioffi | last post: by
4 posts views Thread by Jane Doe | last post: by
18 posts views Thread by Marian F. | last post: by
10 posts views Thread by pamelafluente | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.