So if I look at this correctly, my issue is in the decrypt function with: flag= flag%26;
Do you agree?
No problem, dodgetech - and yes, if you are assigning a letter or a number, based on a negative index of the character set (aka ascii values) - that would be a big red flag, since there are none.
However, I'd still like to know that the encrypt function was working OK, at least in a simple preliminary test, with vowels and consonants.
When you're working with arrays, you always need to test or watch carefully, the ENDS of them (high and low), to see that they don't run over or under, their legit range. Vowels to test would be a and e, and u and y (if you have y as a vowel - some do, some don't). Consonants to test would be b and c and x and z.
We know you're a beginner - but we're not going to let you get away with anything anyway! <smile>
> SORRY, but I have only been programming for 3 months. Before that, I knew absolutely nothing!!!
This is a maths problem, not a programming problem.
The first thing is to understand in mathematical terms is how to turn your expression into something that always yields a positive number.
When you figure out say that adding 26 to flag is the answer, THEN it becomes a programming problem.
You seemed to know enough maths to do the rest of the assignment, unless this is just another "I found some code and it doesn't work" jobs.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
what you really want to get . //?C/C++ Language Programming
well here is what I came up with if anyone is interested:
Code:#pragma warning( disable:4996) #include<stdio.h> //function int FindGCD(int no1,int no2); //for small letters only int InvEuclid(int a,int N) { int r0,r1,r2,q1,x0,x1,x2; r0=N; r1=a; x0=1; r2=r0%r1; q1=r0/r1; x1=-q1; while (r2) { r0=r1; r1=r2; q1=r0/r1; r2=r0%r1; x2=x0-(q1*x1); x0=x1;x1=x2; } if (x0>0) return x0; else return (N+x0); } int main () { char small[27],data[100],cipher[100],decipher[100]; int i=0,j=0,k1,k2,flag,count=0,temp; for (i;i<26;i++){ small[i] = 'a'+i; //printf("%c",small[i]); } // ask user to input message printf("please enter the text message in lower case alphabet ONLY to encrypt\n"); scanf("%[^\n]s",data); fflush(stdin); //ask user to provide at least two mapping i=0; while(1) { printf("\nplease enter the first key 1<k1<26 such that gcd of (k2,26)=1\n"); //first key is: 19 scanf("%d",&k1); printf("\nplease enter the second key 1<k2<26\n "); // second key is: 4 scanf("%d",&k2); flag =FindGCD(k1,26); if (flag==1) break; else printf("\nPlease re-enter the keys"); } //cipher the text and show it to the user while (1) { temp = data[i]; // printf("g%d",flag); if (temp==0) break; flag= data[i]-'a'; if (flag>=0 && flag <=25) { flag=(flag*k1) +k2; printf("%d",flag); cipher[i]=small[flag]; } else { cipher[i]=data[i]; } count++; i++; } // show encrypted text to the user printf("\nthe encrypted string is\n "); while(j<i) { printf("%c",cipher[j]); j++; } printf("\n"); //decipher the ciphered text // printf("c%d\n",count); k1= InvEuclid(k1,26); //printf("%d",k1); i=0; for(i=0;i<count;i++) { flag= cipher[i]-'a'; //printf("%d",flag); if (flag>=0 && flag <=25) { flag=(flag-k2)*k1; printf("%d",flag); decipher[i]=small[flag]; } else { decipher[i]=data[i]; } } j=0; // show user the de-encrypted string printf("\nthe de-encrypted message is: \n "); while(j<i) { printf("%c",decipher[j]); j++; } printf("\n"); return 0; } int FindGCD(int no1,int no2){ int divd,divs,r; if (no1>no2) { divd=no1; divs = no2; } else { divd= no2; divs=no1; } r = divd%divs; while (r>0) { divd = divs; divs = r; r= divd%divs; if (r==1) { return 1; } } if (r==0) { return divs; } return 0; }
Please answer the question, though. Did you test both the functions (encrypt and decrypt), to see whether they both worked properly, on a variety of simple input (and output), that you know the answer to?
Without that test, there's no way to know if your program is working correctly, or not. I certainly can't just look at your code, and expect my muse to come along with the answer, and slap me upside the back of my head with it.
Maybe someone here can just look at it and tell, but really, that's assuming a great deal more than you should, isn't it? You should be the primary tester on your program.
One thing that I noticed -
FAQ > Why fflush(stdin) is wrong - Cprogramming.comCode:fflush(stdin);
FAQ > Flush the input buffer - Cprogramming.com
Hope this helps
Fact - Beethoven wrote his first symphony in C
> well here is what I came up with if anyone is interested:
All you've done is broken it, and added some prints.
Replacing
flag= flag%26;
with
printf("%d",flag);
So as a result, you now have massively overflowed arrays.
DoesCode:$ ./a.out please enter the text message in lower case alphabet ONLY to encrypt hello world please enter the first key 1<k1<26 such that gcd of (k2,26)=1 19 please enter the second key 1<k2<26 4 EncPos=137 EncPos=80 EncPos=213 EncPos=213 EncPos=270 EncPos=422 EncPos=270 EncPos=327 EncPos=213 EncPos=61 the encrypted string is the de-encrypted message is: hello world
cipher[0]=small[137];
cipher[1]=small[80];
cipher[2]=small[213];
look OK to you?
Yes, it comes up with the "right" decoded answer, but it trashed a lot of memory belonging to someone else doing it. Sooner or later, this program would crash in a random way.
I'm bored with trying, so here's the answer.
But only because I doubt you wrote any of that code to begin with. I can't see how you came up with FindGCD() and InvEuclid(), and not be able to figure out this.
Code:flag=(flag-k2)*k1; //printf("g%d",flag); flag= flag%26; if ( flag < 0 ) flag = flag + 26; decipher[i]=small[flag];
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.