Sunday, August 2, 2009

Write a prog. in C to create a string of any length n replace SPACE and @ with % followed by their ASCII value

walk the original string, copying characters to a new string, if a


special character is encountered (SPACE or @) instead copy the ASCII value description.





Inefficient but easy to read--%26gt;





char string1[12] = "Hello World";


char string2[36] = ""; /* enough to convert every character! */





for (i=0, j=0; i%26lt;strlen(string1);i++)


{ if (string1[i] == ' ')


strcat(string2, "%20");


else if (string1[i] == '@')


strcat(string2, "%40");


else


strcat(string2, string1+i);


}





(did I remember the ascii values correctly?)


No comments:

Post a Comment