C++ ENCRYPTION (how to encrypt a text)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jupi13
    New Member
    • Jul 2008
    • 18

    #16
    Code:
    main()
    {
    char string1[10],string2[10],string3[10];
    int x,count;
    cout<<"ENTER TEXT TO COMPACT:";
    cin>>string1;
    count=strlen(string1);
    
    for(int x=0; x<count; x++)
    
      //Check whether x is a multiple of 2
      if(x%2 == 0)
      {
        string2[x/2] = string1[x];
      }
      else
      {
        string3[x/2] = string1[x];
        }
     cout<<string2<<endl;
    
    
    getche();
    }

    i did that but still it onlyy works to text with 4 characters above....
    i couldn't figure out what to do...

    Comment

    • Brosert
      New Member
      • Jul 2008
      • 57

      #17
      Do you get the wrong output for smaller strings, or none at all?

      Comment

      • jupi13
        New Member
        • Jul 2008
        • 18

        #18
        Originally posted by Brosert
        Do you get the wrong output for smaller strings, or none at all?

        i got the wrong output for smaller strings..like if i enter "abc" the
        output is "ac@".... or "ab" i would get "aG@"....

        what should i do with this?

        please help..

        Comment

        • jupi13
          New Member
          • Jul 2008
          • 18

          #19
          Originally posted by Brosert
          Do you get the wrong output for smaller strings, or none at all?

          i think the problem in this is the size of the string...but i realy can't figure ir out...

          Comment

          • jupi13
            New Member
            • Jul 2008
            • 18

            #20
            C++ ENCRYPTION (how to encrypt a text)

            got a problem wtih my encryption...
            would you be kind to help me with this...i'm new to this c++ programing...

            i have this expansion encryption method..where you will add a char in between the original text..example >>>>. josh..this could be >>>> jxoxsxhx...like that...

            my program goes like this


            Code:
            main()
            {
            char string[10],count;
            int x,y;
            
            cout<<"ENTER TEXT:";
            cin>>string;
            count=strlen(string);
            
            for(x=0;x<count;x++)
            
            cout<<string[x]<<string[2]; 
            
            //if i'll enter "JOSH" the output would be "JSOSSSHS"
            //if i'll enter "jupi" the output would be "jpupppip"
            
            getche();
            }


            //what i need to do is to store the output in another array.
            example. array[] will be equal to the cout in the program above which is
            string[x] and string[2]...>>>>> array[x]="string[x]" and "string[2]"

            how will i do that?.....
            how will i do it?
            please help everyone..

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #21
              jupi13 I can see no reason why you restarted your encryption thread. Double posting is against the forum rules and you could clearly see that your original thread was active.

              Anyway please try to avoid starting more than 1 thread on the same subject in future.

              Banfa
              Administrator

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #22
                Part of the problem will all the code samples/examples in this thread is that none of them are correctly handling strings.

                You are using C strings rather than the C++ string class (I assume you have not been taught that yet). In C a string is an array of printable characters with a 0 or NULL terminator. To get the length of a string you should use the function strlen. The operator sizeof returns the length of the array that contains the string not the length of a string.

                Then when using char * pointers to write characters into an array of char once you have finished if you want to use the array as a string then you should make sure that you have written a 0 terminator in the correct place.

                And finally in this piece of code
                Code:
                      int charAt =2;
                       
                      count=strlen(string);
                      //If we have a short input, take the last letter instead of the third
                      if(count <charAt) count=charAt;
                the assignment on line 5 is the wrong way round.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #23
                  Brosert -

                  Please check your private messages

                  Thanks,

                  sicarie

                  Comment

                  Working...