Jump to content

programming question

MisterWhite
Go to solution Solved by Nineshadow,

@MisterWhite

 

Here is my function.

It's pretty simple, it works with usual strings (array of characters) .

 

It modifies the actual string itself , rather than just printing the wanted result. I think it's nicer this way :D.

It takes as input a pointer to a character, basically a character array.

I think it's pretty easy to understand how it works.

Iterate through the array until you reach its end or you find an 'i' . If the next character after that one is 's' , that means we have found an 'is' . We shift the array to the right by 4 starting from the next position , then complete the array with our " not" string.

 

Tested it and it works.

void replaceNot(char *str){    int len = strlen(str);    for(int i = 0 ; i < len; i++)    {        if(str[i] == 'i' && str[i+1] == 's')        {            len+=4;            for(int j = len; j > i+5; j--)                str[j] = str[j-4];            str[i+2] = ' ';            str[i+3] = 'n';            str[i+4] = 'o';            str[i+5] = 't';        }    }}

You use it something like this :

int main(){    char c[100] = "This is right";    replaceNot(c);    cout << c;    return 0;}

So there is a task:

Given a string, return a string where every appearance of the lowercase word "is" has been replaced with "is not".

notReplace("is test") → "is not test"
notReplace("is-is") → "is not-is not"
notReplace("This is right") → "This is not right"

 

i plan on doing this with c++;

i dont need the code of how it's done, just the main concept.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Use regular expressions to check for the "is" and do a replacement of the string.

Guide: DSLR or Video camera?, Guide: Film/Photo makers' useful resources, Guide: Lenses, a quick primer

Nikon D4, Nikon D800E, Fuji X-E2, Canon G16, Gopro Hero 3+, iPhone 5s. Hasselblad 500C/M, Sony PXW-FS7

ICT Consultant, Photographer, Video producer, Scuba diver and underwater explorer, Nature & humanitarian documentary producer

Link to comment
Share on other sites

Link to post
Share on other sites

Use regular expressions to check for the "is" and do a replacement of the string.

 

this is an option. Another option is to stuff every word in a list and print the list recursively. In the loop, check if the word is "is", in which case print "not" right after.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Use regular expressions to check for the "is" and do a replacement of the string.

    string start = "There is an animal named is";    int countLetters = 0;    for(int i=0; i<start.length(); i++)    {        countLetters++;//used if == 2        if( start[i]==' ' || start[i+1]=='\n')// the part with \n does not work        {                if(start[i-3]==' ' && start[i-(countLetters-1)]=='i' && start[i-(countLetters-2)]=='s')                    // 1st condition is to check if after "is" (2 chars) goes ' ' 3rd char                    cout<<"is not ";            countLetters=0;        }    }

so i've made this brute code :D. though now it only prints "is not " if "is" is found (dont know how to include it in the main string). and it does not print if "is" is at the end.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

@MisterWhite

 

Here is my function.

It's pretty simple, it works with usual strings (array of characters) .

 

It modifies the actual string itself , rather than just printing the wanted result. I think it's nicer this way :D.

It takes as input a pointer to a character, basically a character array.

I think it's pretty easy to understand how it works.

Iterate through the array until you reach its end or you find an 'i' . If the next character after that one is 's' , that means we have found an 'is' . We shift the array to the right by 4 starting from the next position , then complete the array with our " not" string.

 

Tested it and it works.

void replaceNot(char *str){    int len = strlen(str);    for(int i = 0 ; i < len; i++)    {        if(str[i] == 'i' && str[i+1] == 's')        {            len+=4;            for(int j = len; j > i+5; j--)                str[j] = str[j-4];            str[i+2] = ' ';            str[i+3] = 'n';            str[i+4] = 'o';            str[i+5] = 't';        }    }}

You use it something like this :

int main(){    char c[100] = "This is right";    replaceNot(c);    cout << c;    return 0;}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

It's pretty simple, it works with usual strings (array of characters) .

Or like this with real strings since it's C++.  It could be made more efficient if you have specific use cases in mind but here's a generic function.

std::string replace(std::string str, std::string sub, std::string repl){    size_t index = 0;    size_t subLen = sub.length();    size_t replLen = repl.length();    while((index = str.find(sub, index)) != std::string::npos)    {        str.replace(index, subLen, repl);        index += replLen;    }    return str;}int main(){    std::string s = replace("is asd is gneirng is not", "is", "is not");    return 0;} 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

 

Or like this with real strings since it's C++.  It could be made more efficient if you have specific use cases in mind but here's a generic function.

 

Ehehe , I'm working on a generic function for it as well. Just with c strings...it's a little more challenging :D

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Or like this with real strings since it's C++.  It could be made more efficient if you have specific use cases in mind but here's a generic function.

 

Bam, and I delivered!

void replace(char *dest , char *find , char *str){    int destlen = strlen(dest);    int strlenn = strlen(str);    int findlen = strlen(find);    bool OK;    for(int i = 0 ; i < destlen; i++)    {        OK = false;        if(dest[i] == find[0])        {            OK = true;            for(int j = 1; j < findlen ; j++)                if( dest[i+j] != find[j]) OK = false;        }        if(OK)        {            int delta = strlenn-findlen;            destlen+=delta;            if(delta >= 0)                for(int j = destlen; j >= i + strlenn; j--)                    dest[j] = dest[j-delta];            else            {                for(int j = i + strlenn; j < destlen; j++)                    dest[j] = dest[j-delta];                dest[destlen] = '\0';            }            for(int j = 0 ; j < strlenn; j++)                dest[i+j] = str[j];            i+=strlenn;        }    }}

It's generic. Works even if the string you are replacing with is shorter :P.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×