Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

//**************************************

//     

//INCLUDE files for :strtran() -- search

//     string for substring then replace it

//**************************************

//     

#include 

#include 

using namespace std;

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

/*

strtran();

----------

this function accepts three arguments:

1: source string

2: search string

3: replacement string

if search string is found to be a substring of

the source string, the substring in the search 

string is replaced by the replacement string.

if the search string is not in the source string

there are no changes.

By:*LuckY*

lucky760@yahoo.com

*/

#include 

#include 

using namespace std;

void shiftright(char*,int);

void shiftleft(char*,int);

void charcpy(char*,const char*);

void strtran(char*,const char*,const char*);

char* substr(char*,const char*);

const char LEN = 80;

    int main (){

    char st[LEN],sr[LEN],rp[LEN];

    cout << "Example of STRTRAN() function\n";

    cout << "-----------------------------\n";

    cout << " Enter a source string: ";

    cin.getline(st,LEN);

    cout << " Enter the search string: ";

    cin.getline(sr,LEN);

    cout << "Enter the replacement string: ";

    cin.getline(rp,LEN);

    strtran(st,sr,rp);

    cout << "\nThe new string is: " << st << endl;

    return 0;

}

// this finds the substring in the sourc

//     e string

char* substr(char* source, 

    const char* substr) {

    char* pstr = source;

    char* psub = (char *)substr;

    int len_str = strlen(source);

    int len_sub = strlen(substr);

        for ( ;len_str >= len_sub;++pstr,--len_str) {

        char *p=pstr;

            while (*p == *psub && *psub) {

            ++p;

            ++psub;

        }

        if (!*psub)

        return pstr;

    }

    return NULL;

}

//find substring,make room for replaceme

//     nt, 

//and insert it

void strtran(char* str, 

const char* srch, 

    const char* repl) {

    unsigned lstr=strlen(str),

    lsrch=strlen(srch),

    lrepl=strlen(repl);

    char* loc=substr(str,srch);

    if (!loc) 

    return; 

    if (lrepl > lsrch) 

    shiftright(loc+lsrch,lrepl-lsrch);

    else if (lrepl < lsrch) 

    shiftleft(loc+lsrch,lsrch-lrepl); 

    charcpy(loc,repl);

}

// make room for daddy 

void shiftright(char* pos,int n) { 

for (int i=strlen(pos);i >= 0;--i)

pos[i+n] = pos[i]; 

}

void shiftleft(char* pos,int n) {

int len=strlen(pos)+1;

for (int i=0;i < len;++i) 

pos[i-n] = pos[i];

}

//copy all but the null character

void charcpy(char* source,

const char* insert) {

int len=strlen(insert);

for (int i=0;i

source[i] = insert[i];

}

 

 

Other 14 submission(s) by this author