Hello all,
I wrote this program that replace a string with another, using my own varaition on string function and now can not get it to work.Please take a look and tell me what I am doing wrong.
I have pasted below the code for all the functions I am using. Thanks in advance.
#include<stdio.h>
#include<string.h>
#define CHARS 30
void main(void)
{
//declare functions
int stringlength (char str[]);
void insertstring (char source[], char str2[], int n);
int findstring (char* s1, char* s2);
void removestring (char *s, int i, int n);
void replacestring (char source[],char str1[], char str2[]);
//declare variable
char text[CHARS];
//copy string to char array text
strcpy(text," 1* for all and all for 1*"
replacestring(text, "1", "one"
replacestring(text, "*", ""
printf("%s\n", text);
}
int findstring(char *s1,char *s2)
{
char *p1=s1,*p2=s2,*tmp;
do {
while ((*p1 != *p2)&&(*p1 != '\0')) p1++;
tmp = p1;
while ((*tmp == *p2)&&(*tmp != '\0')) {
tmp++;
p2++;
}
if (*p2 == '\0') return(p1-s1);
else {
p2 = s2;
p1++;
}
}
while (*(p1-1) != '\0');
return(-1);
}
void removestring(char *s,int i,int n)
{
char *p=s+i;
do {
*p = *(p+n);
if (*p != '\0') p++; /* only bump pointer if not finished */
}
while (*p != '\0');
}
void insertstring(char str1[], char str2[], int i)
{
int y, j, x, len1;
char tmp[20];
len1 = stringlength(str1);
/*copy str1 into tmp*/
for(y = i, x = 0; y <= len1; y++, x++)
tmp[x] = str1[y];
/*copy str2 into str1*/
for(j = 0; str2[j] != '\0'; j++)
str1[i + j] = str2[j];
/*copy tmp into str1*/
for (x = 0; tmp[x] != '\0'; x++)
str1[i + j +x] = tmp[x];
str1[i + j + x] = '\0';
}
void replacestring(char source[], char str1[], char str2[])
{
int i, n;
n = stringlength(str1);
i = findstring(&source, &str1);
removestring(&str1, i, n);
insertstring(source, str2, i);
}
I wrote this program that replace a string with another, using my own varaition on string function and now can not get it to work.Please take a look and tell me what I am doing wrong.
I have pasted below the code for all the functions I am using. Thanks in advance.
#include<stdio.h>
#include<string.h>
#define CHARS 30
void main(void)
{
//declare functions
int stringlength (char str[]);
void insertstring (char source[], char str2[], int n);
int findstring (char* s1, char* s2);
void removestring (char *s, int i, int n);
void replacestring (char source[],char str1[], char str2[]);
//declare variable
char text[CHARS];
//copy string to char array text
strcpy(text," 1* for all and all for 1*"
replacestring(text, "1", "one"
replacestring(text, "*", ""
printf("%s\n", text);
}
int findstring(char *s1,char *s2)
{
char *p1=s1,*p2=s2,*tmp;
do {
while ((*p1 != *p2)&&(*p1 != '\0')) p1++;
tmp = p1;
while ((*tmp == *p2)&&(*tmp != '\0')) {
tmp++;
p2++;
}
if (*p2 == '\0') return(p1-s1);
else {
p2 = s2;
p1++;
}
}
while (*(p1-1) != '\0');
return(-1);
}
void removestring(char *s,int i,int n)
{
char *p=s+i;
do {
*p = *(p+n);
if (*p != '\0') p++; /* only bump pointer if not finished */
}
while (*p != '\0');
}
void insertstring(char str1[], char str2[], int i)
{
int y, j, x, len1;
char tmp[20];
len1 = stringlength(str1);
/*copy str1 into tmp*/
for(y = i, x = 0; y <= len1; y++, x++)
tmp[x] = str1[y];
/*copy str2 into str1*/
for(j = 0; str2[j] != '\0'; j++)
str1[i + j] = str2[j];
/*copy tmp into str1*/
for (x = 0; tmp[x] != '\0'; x++)
str1[i + j +x] = tmp[x];
str1[i + j + x] = '\0';
}
void replacestring(char source[], char str1[], char str2[])
{
int i, n;
n = stringlength(str1);
i = findstring(&source, &str1);
removestring(&str1, i, n);
insertstring(source, str2, i);
}