Saturday, May 22, 2010

Can anyone please help me with this C program??

Write a program to transform its input according to a specified transformation scheme. The transformation scheme will consist of two strings: a string of characters and then a string of replacement characters. The idea is that your program replaces every instance of the ith character in the initial string with the (i+3) character (of English alphabets) in the replacement string. When no substitution is defined for a character, the program just passes it through to the output unchanged. The program should inform the user of any errors in the transformation scheme. Your program should display the phrase before and after the substitutions have been made.


Example:


Original String: This is a C program.


String after the transformation: Wklv lv d F Surjudp.

Can anyone please help me with this C program??
The function is as under:





void transform(char str[100])


{


int i;


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


{


if((str[i]%26gt;=65 %26amp;%26amp; str[i]%26lt;=87)||


(str[i]%26gt;=97)%26amp;%26amp;(str[i]%26lt;=119))


{


str[i]+=3;


}


}


}





NOTE : ASCII code for A is 65 and W is 87, a is 97 and w is 119.





The main function :


void main()


{


char c[100];


printf("Enter a string:");


scanf("%s",c);


printf("Original string:%s",c);


transform(c);


printf("String after the transformation is:%s",c);


}
Reply:I don't normally program in C but I will give you the general idea in Java:


first create an array with the 26 alphabet characters.


so to find the letter we can call the array[n]


where n is the number associated with the letter starting with 0, because arrays always start with index 0.


We need to change the original string so that we can read it.


To do so we need to take each letter and find the length of the whole string.





function String replace(String originalS)


{


int stringLength = originalS.length(); //length of the string


char array1[] = new array[26]; //create the array class with 26


array1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};


//lets make the original String into a char array


char array2[] = originalS.toCharArray();


//now we have filled the array class with the characters


String finalS = new String("");


//we initialize a final string here for later


//now we can do a bunch of for loops to each string character


//this loop will traverse through the original string


for(int i = 0; i %26lt; stringLength; i++)


{


//this loop will traverse through array1 and check it


for(int j = 0; j %26lt; array1[].length; j++)


{


if(array2[j] == ' ')


{


finalS += " "; //we add a space if the original had a space


break; //we break the inserted for loop


}


else if(array2[j].toLowerCase() == array1[j])


{


if(array2[j].isUpperCase())


{


try


{


finalS += array1[j+3].toString().toUpperCase();


}


catch(Exception e)


{


//now we know that the letter is either x, y, or z


finalS += array1[j+3-26].toString().toUpperCase();


}


}


else


{


try


{


finalS += array1[j+3].toString().toLowerCase();


}


catch(Exception e)


{


//now we know that the letter is either x, y, or z


finalS += array1[j+3-26].toString().toLowerCase();


}


}


break; //breaks the inserted for loop


}


else


{


finalS += array2[j].toString();


//if the character is not a letter just keep it the same and add it to finalS


}


}


}


return finalS; //returns the output string


}
Reply:I hope this is the program. If you have problem compiling call me at m_gopi_m@yahoo.co.in





/*


Please create a new folder and place this program in it. It creates


Two text documents(first.txt and second.txt).





first.txt is a copy of the input stream.


second.txt is a copy of encryption.


repalaces ith term by i+3 terms.





Please do not use any special keys like delete,backspace etc...





If you want to change the encryption number. Change 'num' variable(int)


present at 23:14.


*/








#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


FILE *fp1,*fp2;


char a,b;


int num=3,i;





clrscr();


printf("\n\nEnter your message:\n%26lt;When you do so press enter%26gt;");


printf("\n%26lt;Please avoid special characters%26gt;\n\n\t");





fp1=fopen("first.txt","wt+");


while(a!=13)


{


a=getche();


if(a==0)


{


getch();


printf("\b\a");


}


else if(a==8)


{


printf("%c\a",b);


}


else


{


fputc(a,fp1);


b=a;


}


}


rewind(fp1);





fp2=fopen("second.txt","wt+");


while(a!=EOF)


{


a=fgetc(fp1);





if(a!=EOF %26amp;%26amp; a!='\0')


{


if(a!=' ')


a+=num;


fputc(a,fp2);


}


}


fclose(fp1);





rewind(fp2);


clrscr();


printf("\nThe resultant text is\n\n\t");





a=' ';


while(a!=EOF)


{


a=fgetc(fp2);


printf("%c",a);


}





gotoxy(1,22);





for(i=0;i%26lt;13;i++)


printf("%c%c%c%c%c%c",205,205,205,20...


printf("\nFor convenience the input data is saved in first.txt");


printf("\nThe encrypted data is saved in second.txt");


printf("\nPlease take a view at it.");





getch();


fclose(fp2);


}


No comments:

Post a Comment