programming question
Go to solution
Solved by Nineshadow,
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 .
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;}
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 accountSign in
Already have an account? Sign in here.
Sign In Now