Wednesday, March 10, 2010

array of strings

Here is a interesting array of strings function, the output of the function is result of str[0] + str[1]:

int main(void)
{
char str[][40] = { "AAA",
"BBBBBBBB"
};
int strLength[] = {0, 0};
int i;
for(i = 0 ; i<2 ; i++){
while (str[i][strLength[i]])
strLength[i]++;
}

if(sizeof str[0] < strLength[0] + strLength[1] + 1){
printf("\nNo enough space");
}else{
strLength[1] = 0;
while((str[0][strLength[0]++] = str[1][strLength[1]++]));
printf("\n%s\n", str[0]);
}
return 0;
}

No comments:

Post a Comment