Hi,
I am trying to write this program that does a very simple encryption on a string input by the user. It basically looks at the character from the user, checks the 'source' array for that character, and replaces the user's character with the corresponding array character in the 'target' array. So if the first letter in the user's string is 'a', the program finds 'a' in the 'source' string, which is element 1, and would replace it with element 1 from the 'target' array, which is 'k'. It would repeat this for each element in the user entered string. Now what I have below is only as far as the searching of the 'source' array for the match with the user input, which is the 'temp' array.
What Im trying to is have the program search through the entire 'source' array for each element of the user input. If there is no match, it simply continues to the next element of the source array. When it is found, the program increments the input array to the next element, breaks out of the loop and then the while loop starts the for loop over from the begining. This is because I need the next element of the user input array to be searched for from the begining of the 'source' array in case the letter is at the begining of the array. But I need the user input array to hold its place so each increment moves it to the end. So Im tring to use 'static' on the user input pointer, 'temp_ptr' so its not reinitialized when the for loop starts over. But I cant get seem to get it to work. Its value doesn't accend through the input like it should. If you watch the printf statement placed in the for loop, it shows the value of temp_ptr going from element 0 to 1, then back to 0 again. So if the first two letters input were a,b the value of the pointer is going from a then b then back to a. So its not letting the program search properly. How can I cast temp_ptr to static properly here? Any help would be appreciated.
Code:
#include <stdio.h>
main()
{
char source[] = "qazwsxedcrfvtgbyhnujmikolp";
char * src_ptr;
char target[] = "lkjhgfdsaqwertyuiopmnbvcxz";
char * tar_ptr;
char temp[51];
char * temp_ptr;
int counter=0,
i=0,
t=0;
printf("\nPlease enter a line of text: ");
gets(temp);
for(temp_ptr = temp; *temp_ptr != '\0'; ++temp_ptr)
++t;
while(i<t)
{
for(src_ptr = source, temp_ptr = temp; *temp_ptr && *src_ptr != '\0';)
{
if(*temp_ptr != *src_ptr)
{
printf("\n%d nomatch" , counter++);
printf("\n%c" , *temp_ptr); /*shows the position of temp_ptr*/
++src_ptr;
continue;
}
else
{
printf("\n%d match" , counter++);
++temp_ptr;
break;
}
}
printf("\n%c" , *temp_ptr); /*shows the position of temp_ptr*/
++i;
}
getchar();
}