<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3. int cipherLength -> Enter size (2-16) of cipher text to use in encryption
  4. char[cipherLength] cipherText -> Enter the cipher text
  5. int textLength -> Input size (4-256) of text to encrypt including spaces
  6. char[textLength] inputString -> Enter plain text to encrypt
  7. char[textLength] outPut-> Initialize to NULL;
  8. for(int i=0;i<cipherLength;i++){//Loop through the cipher characters
  9.    for(int j=0;j<textLength;j++){//Loop through the plain text characters
  10.    //Find character matches
  11.       if((inputString[j]=cipherText[i]) && (cipherText[i+1] != "\0")){
  12.       //Replace Matches
  13.          outPut[j]=cipherText[i+1];
  14.       }
  15.       //If no character matches
  16.       else{
  17.       //Retain the characters
  18.          outPut[j]=inputString[j];
  19.       }
  20.    }
  21. }
  22. //return the encrypted string of array characters
  23. return outPut;
  24. STOP
  25. ===========================================================================
  26. */
  27.  
  28. /**
  29. * C program to replace all occurrence of a character with another in a string
  30. */
  31. /**
  32. * C program to replace all occurrence of a character with another in a string
  33. */
  34. #include<stdio.h>
  35. #include<string.h>
  36. #define MAX_SIZE 256 // Maximum string size
  37.  
  38. /* GlobalFunction declaration */
  39. void replaceAll(int arrSize,char * array2Txt, char srcChar, char destChar);
  40.  
  41. /*Main function*/
  42. int main()
  43. {
  44.     /*Declaring local Variables*/
  45.     char array2Txt[MAX_SIZE], srcChar, destChar,array1En[16];
  46.     int i,sizeText,sizeCipher;
  47.  
  48.     /*Collecting User Input*/
  49.     printf("\nEnter size of the encryption key 2-16 characters: ");
  50.     scanf("%d",&sizeCipher);
  51.    printf("\nEnter the encryption key in lower case alphabets: ");
  52.     scanf("%s",&array1En);
  53.     printf("\nEnter size of plain text to be encrypted: ");
  54.     scanf("%d",&sizeText);
  55.     printf("\nEnter the plain text to encrypt 4-256 characters+spaces : ");
  56.     scanf("%s",&array2Txt);
  57.     printf("\nString before replacing: \n%s", array2Txt);
  58.  
  59.     /*Looping through the key values and parsing them to the replacement function
  60.     in the appropriate format*/
  61.     for(i=0,array1En[i]!= '\0';i<sizeCipher;i++){
  62.         int j=0;
  63.         /*Restricting the array to the non-null elements*/
  64.         if(array1En[i+1] != '\0'){
  65.             srcChar=array1En[i];
  66.             destChar=array1En[i+1];
  67.         }
  68.         /*Resolving possible null pointer exceptions */
  69.         else if((array1En[i+1]=='\0')){
  70.             srcChar=array1En[i];
  71.             destChar=array1En[j];
  72.         }
  73.         /*Local function call to the globally declared function for replacement*/
  74.         replaceAll(sizeText,array2Txt, srcChar, destChar);
  75.         /*Printing the output in a stepped approach*/
  76.         printf("\n\nStep %d: Replacing '%c' with '%c' : \n%s\n\n",i+1 , srcChar, destChar, array2Txt);
  77.     }
  78.     /*Returning the compiler control on no error*/
  79.    system("PAUSE");
  80.     return 0;
  81. }
  82.  
  83.  
  84. /**
  85. Replacing all occurrences of the given character set srcChar with
  86. Function Implementation
  87. */
  88.  
  89.  /*Global function*/
  90.  void replaceAll(int arrSize, char * array2Txt, char srcChar, char destChar)
  91. {
  92.     int i = 0;
  93.  
  94.     /*Iterating through all characters in the string including spaces */
  95.     while(array2Txt[i] != '\0')
  96.     {
  97.         /* If occurrence of character is found */
  98.         if(array2Txt[i] == srcChar)
  99.         {
  100.             array2Txt[i] = destChar;
  101.         }
  102.         /*Incrementing the Counter*/
  103.          i++;
  104.     }
  105. }