I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to find a chosen substring lets say "cat" and then replace it with a difrent inputed substring say "dog". Tried to get as far as i could on my own but need some help, sugestions, snippets or guidence.
[code=c]...
User Profile
Collapse
-
need help! replacing substing in a string with another substring
-
Thanks for the help i have it figured out with the suggestions, below is a chunk of the code i needed to add, hope others others can learn from it.
[code=c]
char *q;//pointer
int count=0; for (i=0;i<length;i ++)
q[i]=subcap[i];
q=strstr(q+1,s2 );
q=strstr(s1,s2) ;
while (q!= NULL)
{q=strstr(q+1,s 2);
count++;}//counter
[/code] -
No help yet? can anyone tell me where the return value from the strstr goes and am i able to check to see if it is null or not by if statments? Further does the return value to the array need to be 0 or should it be a pointer to s1 or s2?...Leave a comment:
-
help with counting substings in strings???
Im a newby to programing 2 months exp.. I am trying to write code in C to take a input string from the user and then the input substring. I wish to compare the substring to the string and count the number of times it apears and print this to the user. After many hours of frustration i have gotten this far any help or derection would be aprieciated [code=c]
# include<stdio.h >
# include<math.h>
# include<stdlib. h>...
No activity results to display
Show More
Leave a comment: