Write a prog. in C (or C++) to create a string of any length n replace "Special characters" (i.e. characters except a-z, A-Z and 0-9) with % followed by their ASCII values.
Write a prog. in C to create a string of any length n replace Special character with % followed by ther ASCII?
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
char word[64];
int x = 0;
cout %26lt;%26lt; "Enter string:\n";
cin %26gt;%26gt; word;
cout %26lt;%26lt; "converted ascii:\n";
while (word[x] != '\0')
{
int z = __toascii(word[x]);
cout %26lt;%26lt; word[x] %26lt;%26lt; " = " %26lt;%26lt; z %26lt;%26lt; "\n";
x++;
}
cout %26lt;%26lt; "\n";
return 0;
}
Reply:#include%26lt;stdio.h%26gt;
void main()
{
int i;
char str1[100];
printf("\nEnter the number of characters")
scanf("%d",%26amp;n)
printf("\nEnter the characters of the string");
for(i=0;i%26lt;n;i++)
scanf("%c",%26amp;str1[i]);
for(i=0;i%26lt;n;i++)
{
if(str1[i]=='a' || str1[i]='b' || str1[i]='c' || str1[i]='d' || str1[i]='e' || str1[i]='f' || str1[i]='g' || str1[i]='h' || str1[i]='i' || str1[i]='j' || str1[i]='k' || str1[i]='l' || str1[i]='m' || str1[i]='n' || str1[i]='o' || str1[i]='p' || str1[i]='q' || str1[i]='r' || str1[i]='s' || str1[i]='t' || str1[i]='u' || str1[i]='v' || str1[i]='w' || str1[i]='x' || str1[i]='y'|| str1[i]='z' || str1[i]='A' || str1[i]='B' || str1[i]='C' || str1[i]='D' || str1[i]='E' || str1[i]='F' || str1[i]='G' || str1[i]='H' || str1[i]='I' || str1[i]='J' || str1[i]='K' || str1[i]='L' || str1[i]='M' || str1[i]='N' || str1[i]='O' || str1[i]='P' || str1[i]='Q' || str1[i]='R' || str1[i]='S' || str1[i]='T' || str1[i]='U' || str1[i]='V' || str1[i]='W' || str1[i]='X' || str1[i]='Y' || str1[i]='Z' || str1[i]='0' || str1[i]='1' || str1[i]='2' || str1[i]='3' || str1[i]='4' || str1[i]='5' || str1[i]='6' || str1[i]='7' || str1[i]='8' || str1[i]='9')
{
sprintf("%c", str1[i]);
}
else
{
printf("%");
sprintf("%d", str1[i]);
}
}
}
forsythia
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment