Monday, July 27, 2009

Truncating a string in C?

Hello, I was wondering how to truncate a string to the same length as another string if the first string length is more than the second string length. An example would be:





first string = abcdef





second string = abcd





result = abcd abcd





Where two of the strings are seperated by a space.





I was thinking of using something like a for loop:





for(i = 0; str1[i] != '\0' %26amp;%26amp; str2[i] != '\0'; i++){


count++;


count2++;





if(count %26gt; count2){


str1.length = str2.length


}


}





But i'm not sure if that's going to work.


Any help would be appreciated. Thanks

Truncating a string in C?
you can first check if the first string is longer the second string.





if(strlen(str1) %26gt; strlen(str2))


{


for(i = 0 ; i %26lt; strlen(str2) ; i++)


{


your code.


}


}
Reply:if (strlen(str1) %26gt; strlen(str2)) {


str1[strlen(str2)] = '\0';


}

crab apple

No comments:

Post a Comment