Like Tree1Likes
  • 1 Post By Adak

Having a hard time shifting elements

This is a discussion on Having a hard time shifting elements within the C Programming forums, part of the General Programming Boards category; Hello, I am trying my best to shift to the next elment in my array. But its not working. The ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    28

    Having a hard time shifting elements

    Hello,

    I am trying my best to shift to the next elment in my array. But its not working. The first element shifts just fine, but after that, the value from the first moves to the next.The objective of the program is to prompt the user for a string(which UserString02) is, then take that input and compare it to the alphabet of characters and move it n spaces from the character. For example, if someone enters in ABC and also enters in 3, A will be moved to D, B will be moved to E and C will be moved to F. Here is the code I have

    Code:
    for(int i=n-1; i>0;i--) //n is the length of the string
      
      {
       for(int j=0;j<LIMIT;j++) //Limit is the number of characters in the alphabet.
       {
        
        if(UserString2[i]==Alphabet[j])
        {
        
           
           UserString2[i] =Alphabet[k];//k is the amount of spaces the user wants the character to be moved to to be switched to another character.
           UserString2[i]=UserString[i-1];
         
        }
      }
     }
    any help will be greatly appreciated. thanks

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Code:
     UserString2[i] =Alphabet[k];  // adjust this to j+k so u it works even if the letter isn't A
     UserString2[i]=UserString[i-1]; // not sure what u are doing here but it saves over what you just did

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    If it as I understand is Caesar cipher and letters in Alphabet are in alphabet order, then:

    Code:
    for(int i=0; i<n;i++) //n is the length of the string
      
      {
       for(int j=0;j<LIMIT;j++) //Limit is the number of characters in the alphabet.
       {
        
        if(UserString2[i]==Alphabet[j])
        {
        
           
           UserString2[i] =Alphabet[(j+k) % LIMIT];//k is the amount of spaces the user wants the character to be moved to to be switched to another character.
         
        }
      }
     }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Shurik View Post
    If it as I understand is Caesar cipher and letters in Alphabet are in alphabet order, then:

    Code:
    for(int i=0; i<n;i++) //n is the length of the string
      
      {
       for(int j=0;j<LIMIT;j++) //Limit is the number of characters in the alphabet.
       {
        
        if(UserString2[i]==Alphabet[j])
        {
        
           
           UserString2[i] =Alphabet[(j+k) % LIMIT];//k is the amount of spaces the user wants the character to be moved to to be switched to another character.
         
        }
      }
     }
    I don't understand the second nested loop here. The shift is done in one addition or subtraction, for each letter.

    Code:
          //encrypt cipher n is the secret shift number (sssh!) ;)
          n= 5;
       
          for(i=0;i<len;i++) {         //len=strlen(cipher)
             if(isalpha(cipher[i])) {
                if(cipher[i]+n <= 'z')
                  cipher[i]=cipher[i]+n;
                else 
                   cipher[i]=cipher[i]+n-26; 
                
             }else
                cipher[i]=clear[i];      //don't change non alpha char's
          }
       }
       printf("\n\nclear: %s\n\ncipher: %s\n",clear,cipher);
    Lesshardtofind likes this.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Quote Originally Posted by Adak View Post
    Code:
          //encrypt cipher n is the secret shift number (sssh!) ;)
          n= 5;
       
          for(i=0;i<len;i++) {         //len=strlen(cipher)
             if(isalpha(cipher[i])) {
                if(cipher[i]+n <= 'z')
                  cipher[i]=cipher[i]+n;
                else 
                   cipher[i]=cipher[i]+n-26; 
                
             }else
                cipher[i]=clear[i];      //don't change non alpha char's
          }
       }
       printf("\n\nclear: %s\n\ncipher: %s\n",clear,cipher);
    For ASCII only?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Quote Originally Posted by Adak View Post
    I don't understand the second nested loop here. The shift is done in one addition or subtraction, for each letter.
    OK. Super-puper fast version:

    Code:
    for(int i=0; i < n;i++) //n is the length of the string
       for(int j=0; j < LIMIT;j++) //Limit is the number of characters in the alphabet.
         if(UserString2[i] == Alphabet[j])
         {
           UserString2[i] = Alphabet[(j+k) % LIMIT];//k is the amount of spaces the user wants the character to be moved to to be switched to another character.
           break;
         }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Shurik View Post
    For ASCII only?
    I was thinking of a Caesar shift of a plain text message. Like a Cryptogram in the paper. In that case, there's no need to encipher a period or a space or an apostrophe.

    If you want a full enciphering of all the char's, it's the same idea.

    There's still no need to use a double nested for loop. Each char has a value, and each char will then be added by the value of N, (the shift) one time.

    If that addition goes beyond the range of your alphabet, you need to "wrap" the char value, back around, by subtracting 26 (or whatever your alphabetic range is (ascii would be 128), so 'z' + a shift of +1 equals 'a', with the alphabet, or 'z' + 1 equals '[' with full 128 ascii values.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    OK, I understand.
    But what about non-contiguous alphabets like EBCDIC - Wikipedia, the free encyclopedia ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    Quote Originally Posted by Shurik
    But what about non-contiguous alphabets like EBCDIC
    Then obviously another approach should be used, e.g., using an array to store the alphabet in alphabetical order.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-08-2012, 06:12 PM
  2. Deleting and shifting elements in an array
    By sia927777 in forum C Programming
    Replies: 3
    Last Post: 10-13-2010, 02:34 AM
  3. I'm having a hard time making a .exe from a .cpp
    By manugarciac in forum C++ Programming
    Replies: 10
    Last Post: 05-13-2009, 05:40 PM
  4. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  5. shifting array elements
    By dP munky in forum C Programming
    Replies: 5
    Last Post: 04-17-2003, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21