Thread: using static on a pointer

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    using static on a pointer

    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();
    
    }

  2. #2
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    try putting
    Code:
     temp_ptr = temp;
    before entering while(1) loop.I guess That will do it.

    Thanks & Regards
    Vinit.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    using lookup tables indexed by character:

    Code:
    char encryption_table[256];
    char decryption_table[256];
    
    void
    crypt_init(void)
    {
    	for(int i = 0; i < 256; ++i)
    	{
    		decryption_table[(unsigned char)(encryption_table[i] = ~((char)i))] = i;
    	}
    }
    
    char * 
    encrypt(char * str, int * len)
    {
    	char * ret;
    	for(ret = str; *str != 0; ++str)
    	{
    		*str = encryption_table[*str]; 
    	}	
    	if(len)
    	{
    		*len = str - ret;
    	}
    	return ret;
    }
    
    char * 
    decrypt(char * str, int len)
    {
    	char * ret;
    	for(ret = str; len--; ++str)
    	{
    		*str = decryption_table[*str]; 
    	}	
    	return ret;
    }
    
    int
    main()
    {
    	int size;
    	char text[] = "secret message";
    	crypt_init();
    	printf("%s\n", text);
    	printf("%s\n", encrypt(text, &size));
    	printf("%s\n", decrypt(text, size));
    	return 0;
    }
    Last edited by Sebastiani; 04-17-2006 at 01:10 AM. Reason: unsigned char cast needed to guarantee valid index
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    char source[] = "qazwsxedcrfvtgbyhnujmikolp";
    char target[] = "lkjhgfdsaqwertyuiopmnbvcxz";
    And why? When it's clearly equivalent to

    Code:
    char source[] = "abcdefghijklmnopqrstuvwxyz";
    char target[] = "kyasdwtibmvxnoczlqgrpehfuj";
    Besides, using this way, character lookups can be done in constant time. Even better, create a similiar array for deccryption:

    Code:
    char source[] = "abcdefghijklmnopqrstuvwxyz";
    char target[] = "cioevxswhzaqjmnurtdgykfllbp";
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    gets(temp);
    gets dangerous instead use fgets()

    Code:
    fgets(temp,51,stdin);
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM