Thread: Shift letters

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    11

    Shift letters

    Hi to everyone

    Wich funcion should I use if I want to shift all the letters of a word ?

    like if I have AAABC and I want to shift them using a X offset

    X=1

    BBBCD

    X=2

    CCCDE

    I hope someone can help me thanx

    Have a nice day !

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    This will give you the main idea, but you'll still need to
    add some functionality to worry about wrap around
    and vaild inputs etc.
    Code:
    char * shiftStr (char str[], int offset)
    {
      unsigned int i;
    
      for (i = 0; str[i] != '\0'; i++) {
        str[i] += offset;
      }
      return str;
    }
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    ohh thanx man

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    mh, there is a way to say to the program that I want only shift on this letters?

    abcdefghilmnopqrstuvz? (and not xywj and all char > z like @Ô╩─♥ and so on?)

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    I wrote that :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLINE 40
    
    char * shiftStr (char str[], char destination[], int offset)
    
    {
        unsigned int i;
        
        for (i = 0; str[i] != '\0'; i++) {
    
        destination[i] = str[i]+offset;
        
        switch(destination[i]){
        case 'j': destination[i] = str[i]+offset+2;
        break;
        case 'k': destination[i] = str[i]+offset+1;
        break;
        case 'w': destination[i] = str[i]+offset+3;
        break;
        case 'x': destination[i] = str[i]+offset+2;
        break;
        case 'y': destination[i] = str[i]+offset+1;
        break;
        default : destination[i] = str[i]+offset;
        break;
        
        if (destination[i]>'z')
           destination[i]=destination[i]-'z';     
        }
      }
        return destination;
    }
    
    int main(int argc, char *argv[])
    
    {
      char parola[MAXLINE];
      char destination[MAXLINE];
      
      int cont;
    
      scanf("%s", &parola);
      strcpy (destination,parola);
      
      shiftStr (parola,destination,1);
      printf("%s \n",destination);
      shiftStr (parola,destination,2);
      printf("%s \n",destination);
      shiftStr (parola,destination,3);
      printf("%s \n",destination);
      shiftStr (parola,destination,4);
      printf("%s \n",destination);
      shiftStr (parola,destination,5);
      printf("%s \n",destination);
      shiftStr (parola,destination,6);
      printf("%s \n",destination);
      shiftStr (parola,destination,7);
      printf("%s \n",destination);
      shiftStr (parola,destination,8);
      printf("%s \n",destination);
      shiftStr (parola,destination,9);
      printf("%s \n",destination);
      shiftStr (parola,destination,10);
      printf("%s \n",destination);
      shiftStr (parola,destination,11);
      printf("%s \n",destination);
      shiftStr (parola,destination,12);
      printf("%s \n",destination);
      shiftStr (parola,destination,13);
      printf("%s \n",destination);
      shiftStr (parola,destination,14);
      printf("%s \n",destination);
      shiftStr (parola,destination,15);
      printf("%s \n",destination);
      shiftStr (parola,destination,16);
      printf("%s \n",destination);
      shiftStr (parola,destination,17);
      printf("%s \n",destination);
      shiftStr (parola,destination,18);
      printf("%s \n",destination);
      shiftStr (parola,destination,19);
      printf("%s \n",destination);
      shiftStr (parola,destination,20);
      printf("%s \n",destination);
      shiftStr (parola,destination,21);
      printf("%s \n",destination);
      shiftStr (parola,destination,22);
      printf("%s \n",destination);
      shiftStr (parola,destination,23);
      printf("%s \n",destination);
      shiftStr (parola,destination,24);
      printf("%s \n",destination);
      shiftStr (parola,destination,25);
      printf("%s \n",destination);
    
       
    
      system("PAUSE");	
      return 0;
    }

    I think with the red code it can work like I want but it seems to fail... but why ? I still have >z output

  6. #6
    Quote Originally Posted by Cow
    mh, there is a way to say to the program that I want only shift on this letters?

    abcdefghilmnopqrstuvz? (and not xywj and all char > z like @Ô╩─♥ and so on?)
    What do you mean by 'shift' ?

    "Hello" -> "lloHe"

    or

    "Hello" -> "Jgnnq" ('Caesar code' : +2)
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    it is exatly for the 'Caesar code' but for ITALIAN alphabet

  8. #8
    Quote Originally Posted by Cow
    it is exatly for the 'Caesar code' but for ITALIAN alphabet
    What the hell is the Italian alphabet? I'm French, what is the difference with the French alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ?
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    in italian there are not J K W X Y

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider using isalpha() to test for alphabetic characters. I'll assume you want to wrap the characters, so if you "shift" a Z it becomes A, you could always just use isupper or islower, and wrap them with a bit of simple math. I'll leave that up to you, because to provide an example of how to do it, would do all the work for you.

    [edit due to all the alphabetic nonsense you've posted since I started my post...]
    Since you're apparently not using the standard 26 characters, just use a string which contains your "alphabet", and find your letter in it, then shift over by the offset, wrapping around to the start.
    [/edit]

    Quzah.
    Last edited by quzah; 07-01-2004 at 10:11 AM.
    Hope is the first step on the road to disappointment.

  11. #11
    Quote Originally Posted by Cow
    in italian there are not J K W X Y
    What if you process a foreign word ? I'm quite sure that it may occur, even in Italian...

    If you insist, you can use two alphabet references:
    (I guess the digits are left unchanged)

    char const it_aplha_lwr[] = "abcdefghilmnopqrstuv";
    char const it_aplha_upr[] = "ABCDEFGHILMNOPQRSTUV";

    select the letter set with isalpha(), then the uppercase/lowercase subset with isupper() or islower(), then find the letter in the corresponding alphabet with strchr(), compute the position (difference of address), compute the position of the new letter (add n and wrap according to the number of letters of the alphabet), return the letter at the new position.

    Sounds easy. Do your best and post it if you are stuck.

    BTW, it's more a design question than a C one...
    Last edited by Emmanuel Delaha; 07-01-2004 at 10:36 AM. Reason: Addition
    Emmanuel Delahaye

    "C is a sharp tool"

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    it works

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char letters[21] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'z' };
    
    int search(char c);
    
    int main(int argc, char *argv[]) {
    	char *result;
    	int offset, i;
    	
    	char word[40];
    	
    	printf("Inserisci la parola da elaborare: ");
        scanf("%s", &word);
        printf("\n");
    	
    	
    	
    	
    	
    	offset = 1;
        
        while(offset<21){
    	result = (char *) calloc(strlen(word) + 1, sizeof(char));    
        for (i = 0; word[i] != '\0'; i++)
    		if (word[i] == ' ')
    			result[i] = ' ';
    		else
    			
                result[i] = letters[(search(word[i]) + offset) % 21];
    	           
                
    	printf("%s\n", result);
    	free(result);
        ++offset;
        }
    	printf("\n");
    	system("PAUSE");
    	return 0;
    }
    
    int search(char c) {
    	int i;
    	for (i = 0; i < 21; i++)
    		if (letters[i] == c)
    			return i;
    	return -1;
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your search function is flawed. Your problem comes when you encounter a character that is not in the alphabet. In this case, your search function returns -1.

    This, combine with the way you're using it, will always give you 'a'.

    If you are trying to "encode" a message, it will be impossible to "decode" it back to the origional because of this.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    Quote Originally Posted by quzah
    In this case, your search function returns -1.
    I know, I will surely add something to control it, anyway except human errors, I wil never have to analyze not italian words

    now i'm trying to add a cool thing, I bet I will ask help again in some hours lol

    thanx everytone for your help

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    Ok my program works but I want to add a dictionary control.
    I have the "masked" word, I write it in my program and it creates 20 new words. ONE of this is the word I was looking for.
    I want to search that word using a dictionary.
    I tried to add to my program some old code I wrote time ago :

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main(void){
    FILE *in;
    char *s;
    char cerca[20];
    in=fopen("pippo.plu","r");
    scanf("%s",&cerca);
    while(!feof(in))
    {
    fscanf(in,"%s",s);
    if(strstr(cerca,s))printf("trovato");
    }
    system("pause");
    }
    (working!)

    But when I put it in my last program, everything crushes...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char letters[21] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'z' };
    
    int search(char c);
    
    int main(int argc, char *argv[]) {
    	char *result;
    	int offset, i;
    	FILE *dizionario;
        char *s;	
    	char word[40];
    		
    	printf("Inserisci la parola da elaborare: ");
        scanf("%s", &word);
        printf("\n");
    	
    	offset = 1;
        
        while(offset<21){
    	result = (char *) calloc(strlen(word) + 1, sizeof(char));    
        for (i = 0; word[i] != '\0'; i++)
    		if (word[i] == ' ')
    			result[i] = ' ';
    		else
    			
                result[i] = letters[(search(word[i]) + offset) % 21];
    	           
                
    	printf("%s  offset %2d", result,offset);
    	
        dizionario = fopen("dizionario.txt","r");
    
        while(!feof(dizionario))
    	{
    	
        fscanf(dizionario,"%s",s);
    	
        if(strstr(result,s))
           printf("  <-- found -->  %s",result);
            else
             printf("\n");
    	}
    	
    	fclose (dizionario);	free(result);
        ++offset;
        }
    	printf("\n");
    	system("PAUSE");
    	return 0;
    }
    
    int search(char c) {
    	int i;
    	for (i = 0; i < 21; i++)
    		if (letters[i] == c)
    			return i;
    	return -1;
    }
    I really dont know why... It seems all ok but obviously it is not..
    Can u see where is the mistake??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. Ceasar Shift program
    By trevordunstan in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 09:40 PM
  3. Caeser Cipher
    By L_U_K_E in forum C++ Programming
    Replies: 35
    Last Post: 06-30-2006, 07:15 AM
  4. Simulating Shift Key
    By PrimeTime00 in forum Windows Programming
    Replies: 7
    Last Post: 10-15-2004, 05:53 AM
  5. circular shift
    By n00bcodezor in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 03:51 AM