Thread: next wierd thing

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    next wierd thing

    i have the following
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int myfunc( char *, char *, int , int , int );
    
    int main()
    {
        char text[] = "tomorrow never dies";
        char pword[] = "hel";
        char encrypted[sizeof text];
    
        int len = strlen( text );
        int plen = strlen( pword );
    
        for ( int j = 0, i = 0; text[i]; i++, j = (j + 1) % plen )
            encrypted[i] = text[i] ^ pword[j];
        encrypted[len] = '\0';
    
        int Guesses[ sizeof pword ][26] = { 0 };
        // Don't use numbers for letters, e.g., say 'A' instead of 65.
        for ( int StartIndex = 0; StartIndex < plen; StartIndex++ )
        {
            int GuessIndex = 0;
    
            for ( int i = 'a'; i <= 'z'; i++ )
            {
                char c = i;
                if ( myfunc( encrypted, &c, StartIndex, len, plen ) )
                {
                    Guesses[StartIndex][GuessIndex] = i;
                    GuessIndex++;
                    //c = 'a';
                }
            }
            //putchar('\n');
        }
    if ( myfunc(encrypted, pword, 0, len, plen) )
            printf("Password found!!\n ");
    
        return 0;
    }
    
    int myfunc( char *Encrypted, char *c, int StartIndex, int len, int plen )
    {
        int Letter = 1;
    
        for ( int i = 0; i < strlen( c ); i++)
            for ( int j = StartIndex; j < len; j += plen )
            {
                int x = Encrypted[j] ^ c[i];
                if ( ! ( ( x >= 'A' && x <= 'Z' ) || ( x >= 'a' && x <= 'z' ) || (  x >= ' ' && x <= '@' ) ) )
                {
                    Letter = 0;
                    break;
                }
            }
        return Letter;
    }
    however i find the first possible letter of the password with c being one character long then for some reason it remembers the first character found and appends it to all the others so watching c in debug mode i get c= a, b, c..... ,h , ih, jh, kh etc etc.

    why is it remembering the first letter found also if i comment out the guess's stuff c becomes a random bunch of letters its not even i

    the idea is once i have a list of possible passwords i can test them by making sure all the letters work together as they did singularly
    Last edited by cooper1200; 06-06-2023 at 03:41 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,664
    c is just a single char, not an array of char.
    You pass it's address to myfunc and then use strlen on it, which you definitely shouldn't do! (Strings need to be '\0' terminated and the only way c can be properly terminated is if it is '\0' itself.)
    Maybe make c an array of 2 chars, ensuring that the second is '\0'. Then just pass c, not &c.
    Code:
                char c[2] = { i, '\0' }; // or just { i } since extra elements are zeroed
    All truths are half-truths. - A.N. Whitehead

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd..
    By Mitsukai in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-20-2006, 03:21 PM
  2. huh wierd
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2004, 01:40 PM
  3. This is wierd.
    By Ranedhel in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2003, 03:26 PM
  4. wierd !!
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 11-15-2003, 05:19 AM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM

Tags for this Thread