Saturday 2 February 2013

A simple way to insert a char into a string

This following code is used to insert a char into *str without declaring any temporary string variable:
Shift each char in the *str from last char to the current position to the one right position(move the '\0' first untill the current postion)
Hope you can get inspiration from this segment:
int i;
int lcstrPos; //Cursor Position in the string
int strLength;

strLength = strlen(str);
lcstrPos =  *curPosition + *strOffset;
for(i=strLength+1;i>lcstrPos;str[i--]=str[strLength--]);
str[lcstrPos] =  key; 


Please leave your comment if any problems.

No comments:

Post a Comment