Thread: keyword replacement

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    4

    keyword replacement

    Im trying to make a keyword replacement program. I want to be able to enter a keyword into the program which will then change move the letters of the alphabet around:

    If the word cipher was used the alphabet would look like this:

    abcdefghijklmnopqrstuvwxyz

    cipherzyxwvutsqonmlkjgfdba

    If my name was used the alphabet would look like this:

    abcdefghijklmnopqrstuvwxyz

    jackzyxwvutsrqponmlihgfedb

    Below is a function ive made for encoding the a message that has been entered into the string message[333].

    If anyone could give me some advice I would really appreciate it, Ive had a lot of sleepless nights with this one!

    One more thing, this is an assignment set by my college and I dont want anyone to do my homework for me, im just looking for a bit of advice.

    Thanks!
    Jack

    Code:
    /*FUNCTION encodemessage()*/
    
    char encodemessage()
    
    {
    char 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',' '};
    char array2[80];
    char key[333];
    char newmessage[333];
    int a;
    int b;
    b = strlen(array1);
    int q;
    int r;
    r = strlen(message);
    int x;
    int y;
    y = strlen(key);
    int i;
    
    	clrscr();
    	printf("%s", message);
    	printf("\nenter key for cipher: ");
    	gets(key);
    	
    	for(x=0;x<y;x++){
    		
    		for(a=0;a<b;a++){
    			
    			if(array1[a]==key[x]){
    				array1[a]='*';		
    			}
    		}
    
    	}
    				strcpy(array2,array1);
    				strcat(key, array2);
    
    
    	for(q=0;q<r;q++){
    		if(message[q]==array1[i] && message[q]!='*' && message[q]!=NULL && array1[i]!=NULL && key[i]!=NULL)
    				message[q]=key[i];				
    		
    			else
    				i++;
    	}
    				
    
    		
    strncpy(newmessage, message, q);
    printf("\n\n\n%s", newmessage);
    return 0;

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    4
    Ive made a couple of changes to silly mistakes, but im still not getting any results.

    Code:
    char encodemessage()
    
    {
    char aarray[]={'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',' '};
    char array1[]={'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a',' '};
    
    char key[333];
    char newmessage[333];
    int a;
    int b;
    b = strlen(array1);
    int q;
    int r;
    r = strlen(message);
    int x;
    int y;
    y = strlen(key);
    int i;
    
    	clrscr();
    	printf("%s", message);
    	printf("\nEnter key for cipher: ");
    	gets(key);
    	
    	for(x=0;x<y;x++){
    		
    		for(a=0;a<b;a++){
    			
    			if(array1[a]==key[x]){
    				array1[a]='*';		
    			}
    		}
    
    	}				
    
    	strcat(key, array1);
    
    
    	for(q=0;q<r;q++){
    		if(message[q]==aarray[i] && message[q]!=NULL aarray[i]!=NULL && array1[i]!=NULL && key[i]!=NULL){
    				message[q]=key[i];	
    		}
    	
    			else
    				i++;
    	}
    				
    
    		
    strncpy(newmessage, message, q);
    printf("\n\n\n%s", newmessage);
    return 0;

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You set y to the strlen of key before key is initialized.
    You should do this after gets. (you should also use fgets instead of gets)

    You use i uninitialized.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also array1 is not null-terminated string - it is just a char array, you cannot use strlen on it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's much easier to write this
    Code:
    char aarray[]={'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',' '};
    as this
    Code:
    char aarray[] = "abcdefghijklmnopqrstuvwxyz ";
    and you get a NULL terminator in the second version, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by JackBear View Post
    Ive made a couple of changes to silly mistakes, but im still not getting any results.

    Code:
    char encodemessage()
    
    {
    char aarray[]={'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',' '};
    char array1[]={'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a',' '};
    
    char key[333];
    char newmessage[333];
    int a;
    int b;
    b = strlen(array1);
    int q;
    int r;
    r = strlen(message);
    int x;
    int y;
    y = strlen(key);
    int i;
    
    	clrscr();
    	printf("&#37;s", message);
    	printf("\nEnter key for cipher: ");
    	gets(key);
    	
    	for(x=0;x<y;x++){
    		
    		for(a=0;a<b;a++){
    			
    			if(array1[a]==key[x]){
    				array1[a]='*';		
    			}
    		}
    
    	}				
    
    	strcat(key, array1);
    
    
    	for(q=0;q<r;q++){
    		if(message[q]==aarray[i] && message[q]!=NULL aarray[i]!=NULL && array1[i]!=NULL && key[i]!=NULL){
    				message[q]=key[i];	
    		}
    	
    			else
    				i++;
    	}
    				
    
    		
    strncpy(newmessage, message, q);
    printf("\n\n\n%s", newmessage);
    return 0;
    Im trying to make a keyword replacement program. I want to be able to enter a keyword into the program which will then change move the letters of the alphabet around:

    If the word cipher was used the alphabet would look like this:

    abcdefghijklmnopqrstuvwxyz

    cipherzyxwvutsqonmlkjgfdba

    If my name was used the alphabet would look like this:

    abcdefghijklmnopqrstuvwxyz

    jackzyxwvutsrqponmlihgfedb

    Below is a function ive made for encoding the a message that has been entered into the string message[333].

    If anyone could give me some advice I would really appreciate it, Ive had a lot of sleepless nights with this one!

    One more thing, this is an assignment set by my college and I dont want anyone to do my homework for me, im just looking for a bit of advice.

    Thanks!
    Jack
    Hi Jack,
    Looks like there are two main parts to your assignment's algorithm:

    1) Take the letters from the added word, and print them first, in order.

    Then

    2) Take all the remaining letters, and print them in descending order (z - a)

    #1 is obviously trivial, agreed?

    #2 could be dealt with simply by using an array of 26 char's, with the letters of the alphabet (which you have) already.

    Simple example: the input word is "abe", so we print:
    abe

    In the alphabet array, a is in alpha[0], because it's the first letter of the alphabet. So we set alpha[0] = 1, signifying that the a has already been used or printed. We do the same thing with alpha[1] or b, and alpha[4] or e. (a==0. b == 1, c==2,... e ==4)

    Now, in a for loop (for instance), we scan through the alpha array. from the last element in the array, alpha[25], to alpha[0]. If the alpha [i] == 0, then we need to print the letter which corresponds to the subscript of alpha[i]. If the alpha[i] == 1, then we print nothing and go to the next lower letter.

    The main thing is to keep it simple and clear as a bell. (And not use so many cliche's, but that's my problem)

    Let me know if that makes sense to you or not. Don't worry about the syntax issues yet. Let's get the algorithm right, and then we'll flush out all the details.

    As the letters are printed, they can simply be added into a second key [] array, in their proper order, from key[0] to key[25]. That's just a detail, really.

    Adak
    Last edited by Adak; 05-17-2007 at 02:13 AM.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    4

    new plan

    im trying out the next bit of my assignemt which is to take the message and change it according to the rearranged letters. This is what ive come up with so far. Any hints as to why this isnt working?

    Code:
    #include <stdio.h>
    #include <dos.h>
    #include <math.h>
    #include "bcio2.h"
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    
    char alpha[]  ={'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',' ',};
    char cipher[] ={'c','i','p','h','e','r','z','y','x','w','v','u','t','s','q','o','n','m','l','k','j','g','f','d','b','a',' ',};
    char message[]={'t','h','i','s',' ','i','s',' ','a',' ','m','e','s','s','a','g','e'};
    int a;
    int b = strlen(alpha);
    int x;
    int y = strlen(message);
    
    main()
    {
    	printf("%s\n", message);
    	printf("%s\n", alpha);
    	printf("%s\n", cipher);
    
    	for(x=0;x<y;x++){
    		for(a=0;a<b;a++){
    			if(message[x]==alpha[a] && message[x]!=NULL)
    			message[x]=cipher[a];
    			printf("message %c is alpha %c so message %c is now cipher %c\n", message[x],alpha[a],message[x],cipher[a]);
    		
    		}
    		
    		
    	}
    
    		
    	//	printf("%d", x);
    	//	printf("%d", a);
    		
    		return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Jack, you're carrying on with a hardcoded key example, when your program needs a softcoded one, using logic that will not do what you want, (although I don't quite grok your algorithm, so who knows?).

    You need to get your cipher[] (which is not a cipher, but a key, and the naming may confuse you and everyone else), working right, before trying to expand to the rest of your program. The cipher is the output from the product of the plaintext message and the key.
    plaintext + key == ciphertext.

    Once you get your algorithm right, then all the details can get nailed down.

    Take some time, work it out by hand, and then write down your steps you're using to get it done. Big steps become functions, little steps go inside the big steps if they're a good functional fit.
    Last edited by Adak; 05-18-2007 at 12:17 PM.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    4
    thanks for you insight, im just panicking at the moment! ill do as you suggested and sit down again and write it out in and.

    Jack.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I recommend a cull on your headers

    Code:
    #include <stdio.h>
    #include <dos.h>
    #include <math.h>
    #include "bcio2.h"
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    The only ones your using are,
    Code:
    #include <stdio.h>
    #include <string.h>
    Also read the FAQ regarding main(), int main(), void main(). Although main() == int main(void) you should really ensure it is

    Code:
    int main(void)
    {
    /* ... */

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Jack,
    At some point in your steps you're going to want to "take all the letters not used int he portion of the key that was entered, and put them
    backwards onto the end of the entered portion.

    A good simple way to do that is to have a Used [] array of char's. All the letters will just fit in it, and so every char that gets used in the entered
    portion of the key, will be recorded into the used array. Just a 0 (that letter wasn't used), or a 1 (it was used), is all it will hold.

    Say the entered key was "key". Our used [] would look like this:
    Code:
    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 -letters represented
    0 1 2 3 4 5 6 7 8 9 10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25 - subscripts "
    0 0 0 0 1 0 0 0 0 0  1   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0  - value in array
    You don't have to do it just this way, at all, but you do have to reach the same goal, in the end.

    edit: Good catch Vart, we need the keyword to have the null char right after the last letter: '\0' .

    edit 2: This was what I used for a final assignment into the key array,
    (Not the key letters entered by the user, but all the other letters that the user didn't enter).
    Code:
    for (i = Size - 1, j - i; i >= 0; i--)  {
       if (used[i] == 0)  {
          key[j] = alpha[i];
          j--;
       }
    }
    IMO, the j variable makes this a pretty sly dog, indeed.

    Note that as with any single alphabetic substitution, the key the user enters, can have no duplicates.
    Last edited by Adak; 05-19-2007 at 04:38 PM.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you are still using strlen on the not-null terminated array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delayed Replacement Sort
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2007, 03:36 PM
  2. Virtual Keyword
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2006, 11:37 AM
  3. NeHe Glaux replacement??
    By Razorblade Kiss in forum Game Programming
    Replies: 4
    Last Post: 02-25-2006, 03:30 AM
  4. how to search a keyword in C?
    By kreyes in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 08:22 PM
  5. keyword searching in C
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 02:26 AM