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.
12 28650
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
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 :- - 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.
-
-
}
-
}
-
-
}
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
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.
-
-
}
-
}
-
-
}
Without using c code, tell me your algorithm. I think it will make it clearer to both you and me.
Adrian
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.
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
This aint'a gonna work: -
char* str = "this is string";
-
str is a constant. The compiler will never let you change a literal. Use a non-const array instead: -
char str[] = "this is string";
-
Here you have a char array initialized with the string.
Now have fun.
This aint'a gonna work: -
char* str = "this is string";
-
str is a constant. The compiler will never let you change a literal. Use a non-const array instead: -
char str[] = "this is string";
-
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
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
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
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
- #include <stdio.h>
-
#include <conio.h>
-
#include <string.h>
-
void main()
-
{
-
char a[50],b[10],c[10],d[50],t[50];
-
int n,m,i,o,j,l,k=0,l1=0;
-
clrscr();
-
printf("\n\nType the sentence :");
-
gets(a);
-
printf("\n\n\nsub string :");
-
gets(b);
-
printf("\n\n\nReplacing :");
-
gets(c);
-
n=strlen(b);
-
o=strlen(a);
-
m=strlen(c);
-
for (i=0;i<o;i++)
-
{
-
for(j=0;j<n;j++)
-
t[j]=a[i+j];
-
t[j]='\0';
-
if (strcmp(t,b)==0)
-
{
-
k++;
-
for(l=0;l<m;l++)
-
{
-
d[l1]=c[l];
-
l1++;
-
}
-
i=i+(n-1);
-
}
-
else
-
{
-
d[l1]=a[i];
-
l1++;
-
}
-
}
-
d[l1]='\0';
-
printf("\n\n\nThe number of string =%d",k);
-
printf("\n\n\nAnswer=%s",d);
-
getch();
-
}
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
|
2 posts
views
Thread by Jon Maz |
last post: by
|
4 posts
views
Thread by Jane Doe |
last post: by
|
reply
views
Thread by hari krishna |
last post: by
|
18 posts
views
Thread by Marian F. |
last post: by
|
10 posts
views
Thread by pamelafluente |
last post: by
|
4 posts
views
Thread by hanseymoon |
last post: by
| | | | | | | | | | | | |