473,372 Members | 1,049 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 29297
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

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

Similar topics

3
by: Chris Cioffi | last post by:
I started writing this list because I wanted to have definite points to base a comparison on and as the starting point of writing something myself. After looking around, I think it would be a...
2
by: Jon Maz | last post by:
Hi, I have written a page to test a function of mine that strips non-English accents and various other characters out of a given string. The function itself (called 'StripAll' in the code below)...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
0
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is...
18
by: Marian F. | last post by:
The 12 years old genius function to count english words in a sentence: ' This is my function to count english words in your string ' s is the string with your words to be counted ' Returns an...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
4
by: hanseymoon | last post by:
Dear newsgroup: I've got this long function, which works good overall to spell check words from a dictionary and I am not in a position to replace it. Can someone please see where or how it...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
7
by: abcmavdi | last post by:
I want to write a code which replace words from string.without use of string function in c language. Example str = "This is string" remove "is" from string and replace with "was". so, finally str...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.