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?)

Write a prog. in C to create a string of any length n replace SPACE and @ with % followed by their ASCII value
I once did this in C++, which is pretty similar to C. I replaced the word "dog" with the word "cat".





char sentence[100] = "I have a fat dog";


int step = 0;





for(step = 0, step %26lt;=100, step ++)


{





if (sentence[step] == 'd' %26amp;%26amp;


sentence[step + 1] == 'o' %26amp;%26amp;


sentence[step + 2] == 'g')


{


sentence[step] = "c";


sentence[step + 1] = "a";


sentence[step + 2] = "t";


}








}


No comments:

Post a Comment