0

I wrote this simple encryption code where First, each uppercase, or lowercase letter must be shifted three positions to the right, according to the ASCII table: letter 'a' should become letter 'd', letter 'y' must become the character '|' and so on. Second, each line must be reversed. After being reversed, all characters from the half on (truncated) must be moved one position to the left in ASCII. In this case, 'b' becomes 'a', and 'a' becomes '`'

My code is here:

#include <bits/stdc++.h>
#include<string.h>
using namespace std;

int main()
{
    char s[10000],b[10000];
    int a,i,len,j,c,decre,divition,x;
    scanf("%d",&a);
    for(i=1;i<=a;i++)
    {
        len=0;
        c=0;
        decre=0;


        for(x=0;x<1000;x++)
        {
            s[i]='\0';
            b[i]='\0';
        }

        scanf(" %[^\n]s",s);
         len=strlen(s);
        //printf("%d",len);
        for(j=0;j<len;j++)
        {
            if((b[j] >= 'a' && b[j] <= 'z') || (b[j] >= 'A' && b[j] <= 'Z')||(b[j]>=123&&b[j]<=126))
               {

                c=s[j];
                s[j]=c+3;
               }
        }
        for(j=len-1;j>=0;j--)
        {
            b[decre]=s[j];
            decre++;
            //printf("%d\n",j);
        }
        divition=len/2;
        for(j=divition;j<len;j++)
        {
            if((b[j] >= 'a' && b[j] <= 'z') || (b[j] >= 'A' && b[j] <= 'Z')||(b[j]>=123&&b[j]<=126)||(b[j]>=33&&b[j]<=47)||(b[j]>=91&&b[j]<=96))
               {

                    c=b[j];
                    b[j]=c-1;
               }
        }

        printf("%s\n",b);
    }
}

So if i input such a it will output c.

Input:

4
Texto #3
abcABC1 
vxpdylY .ph 
vv.xwfxo.fd

Output:

3# rvzgV
1FECedc
ks. \n{frzx
gi.r{hyz-xx
My output:
3# oswdS
4FECbaS
hp.#Yn{crzu 
Gi1rxhvw-xx
David Buck
  • 3,594
  • 33
  • 29
  • 34
ahoy ray
  • 13
  • 2
  • Your task will be much easier to complete if you use actual C++ functionality rather than C that you are using now. Why not switch? – atru May 24 '20 at 14:44
  • @artu the ASCII table is pretty big to handle with switch case that's why. Thanks – ahoy ray May 24 '20 at 14:46
  • 1
    You should use [std::isalpha](https://en.cppreference.com/w/cpp/string/byte/isalpha) instead of `(b[j] >= 'a' && b[j] <= 'z') || (b[j] >= 'A' && b[j] <= 'Z')`. You shouldn't use C headers with C++ (`#include`) – Thomas Sablik May 24 '20 at 15:00
  • @ahoy ray I'm talking about the use of char arrays rather than std::string and scanf instead of std::cin. Are you familiar with these? – atru May 24 '20 at 15:00
  • 1
    Did you try to run your program in a debugger? – Thomas Sablik May 24 '20 at 15:06
  • 1
    Artu thanks, it was my fault, i didn't concentrate what you were saying. yes i am familiar with those and thanks for your advice – ahoy ray May 24 '20 at 15:15
  • What i am asking is there any logical error? because i must have messed up in some logic, right? that's what i want to find out.. thanks again – ahoy ray May 24 '20 at 15:17
  • 3
    Yes, there obviously is a logical error because your actual output differs from your expected output. The best way to find logical errors is to start your program in a debugger and step through your code. Compare the actual values with your expectations and find the problem. – Thomas Sablik May 24 '20 at 15:22
  • 2
    [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl May 24 '20 at 15:25
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl May 24 '20 at 15:26

0 Answers0