# Thread: Very simple Encryption problem. My code is not working acording to my logic.. see it.

1. ## Very simple Encryption problem. My code is not working acording to my logic.. see it.

Ok, before I startlet me describe what the program that i'm writing needs to do..

1. The program asks the user to enter a 10-letter word.

2. Each letter/character entered, is encrypted by changing it to the next adjacent letter of the alphabet.

eg. if 'a' is entered, it is changed to 'b'
eg, if the word "dumb" is entered, it is changed to "evnc"

Now, I've written a code to do this, but it doesn't seem to work well. It is returning the word that i'm inputting same to same
(i.e, no encryption is taking place.)

here is my code:

Code:
```
# define n 26

int main()
{
char a[10];
char b[n]={'a', 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int i,j;

printf("Please type your sentence to be Encrypted below: \n\n");

for(i=0; i<10; i++)
{
scanf("%c", &a[i]);
}

for(i=0; i<10; i++)
{
printf("%c", a[i]);
}

for(j=0; j<n; j++)
for (i=0; i<10; i++)
{
if (a[i]!=b[j])
{
j++;            // if there is no match, increment j and continue comparing
}

else                  //after a match is found,  assign the content at b[j+1] to a[i]
{                   // then print a[i]
a[i]=b[j+1];
printf("%c", a[i]);
i++;
}
}

return 0;
}```

Whats wrong with this code?

If, this is logically not good, do you know any better way of doing it??

If so, suggest me so that I can write the program and make it generated the correct output..

Any help/suggestion is greatly appreciated.

2. Whoa! You're looping through every letter of the alphabet and painstakingly checking to see if every letter in the message is equal to that letter. "Is the first letter 'a'? Is the second letter 'a'? Is the ... etc etc ... is the first letter 'b'? Is the second letter 'b'"?

Why would you do this? Why not just loop through the message once and say a[i]++ for each letter? There should be no need to compare anything with an array containing the whole alphabet. There should be no need for nested loops.

You'll eventually need to deal with capital letters, non-alphabet characters and how to handle 'z', but that's still some way off.

3. First of all, a is not big enough to hold that string, you need a buffer that can hold the trailing '\0'. Second, you can use scanf("s", &a), there is no need to get one character at a time. Third, there is no need to construct a table of characters since they all have values one after another in the ASCII table.

4. Originally Posted by TheBigH
Whoa! You're looping through every letter of the alphabet and painstakingly checking to see if every letter in the message is equal to that letter. "Is the first letter 'a'? Is the second letter 'a'? Is the ... etc etc ... is the first letter 'b'? Is the second letter 'b'"?

Why would you do this? Why not just loop through the message once and say a[i]++ for each letter? There should be no need to compare anything with an array containing the whole alphabet. There should be no need for nested loops.

You'll eventually need to deal with capital letters, non-alphabet characters and how to handle 'z', but that's still some way off.

Ok, is this what you mean??

Because the program is functioning as I want it..

Code:
```

int main()
{
char a[10];

int i,j;

printf("Please type your sentence to be Encrypted below: \n\n");

for(i=0; i<10; i++)
{
scanf("%c", &a[i]);
}

for(i=0; i<10; i++)
{
printf("%c", a[i]);

}

printf("\n\n\n\n");
printf("The Encrypted text is: ");

for(i=0; i<10; i++)
{
a[i]++;
printf("%c", a[i]);

}

printf("\n\n\n\n");

return 0;
}```
But, as you said, handling 'z' is another case.. Any idea?

However, I wish to ignore any spaces and special characters.

5. Originally Posted by pprav05
Ok, is this what you mean??

Because the program is functioning as I want it..
like this....

Code:
```#include <stdio.h>
#include <string.h>

int main()
{
char uText[100];

int i,j;

printf("Please type your sentence to be Encrypted below: \n\n");
fgets(uText,99,stdin);
printf("%s\n\n",uText);
printf("The Encrypted text is: \n ");

j = strlen(uText);

for(i=0; i<j; i++)
uText[i]++;

printf("%s\n\n", uText);

getchar();

return 0;
}```

6. Originally Posted by pprav05
But, as you said, handling 'z' is another case.. Any idea?

However, I wish to ignore any spaces and special characters.
Okay, so if your letter is a-y or A-Y you want to do the normal thing.
If it's z or Z, do something else; I assume you'll want to loop back to the start of the alphabet.
If it's anything else, do nothing.

You can quite easily translate the above three sentences into code.

7. Here ya go... decent encryption in 60 lines or less...
Code:
```// file encrypt and decrypt example
// encrypt and dectypt using char + key * 2
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{ char  crypt;  // encryption key
short fval;   // file values
FILE  *infile, *outfile;

if(argc < 4)
{ printf("File encryptor/dectyptor...\n");
printf("Encrypt or decrypt a file based based on a secret method\n\n");
printf("Usage : \n");
printf("     crypt <key> <sourcefile> <outputfile>\n\n");
printf("Key :\n");
printf("   To encrypt enter a single character as your encryption key\n");
printf("   To decrypt enter * as the key\n\n");
printf("Examples :\n");
printf("     crypt x mywork.txt safework.txt\n");
printf("     crypt * safework.txt homework.txt\n\n");
exit(255); }

// open the files
infile = fopen(argv[2],"rb");
if (! infile)
{ printf("Unable to open input file\n\n");
exit(254); }

outfile = fopen(argv[3],"wb");
if (! outfile)
{ printf("Unable to open output file\n\n");
exit(253); }

// active encryptor
if ((argv[1][0] == '*'))
{ printf("Decrypting...\n\n");
// first value in the file is the key
if ( fread(&crypt, sizeof(char), 1, infile) )
{ crypt = ~crypt;
while( fread(&fval ,sizeof(short), 1, infile) )
fputc( (fval / 2) - crypt, outfile );  } }
else
{ printf("Encrypting...\n\n");
// set key as first value in file}
crypt = ~argv[1][0];
fwrite(&crypt, sizeof(char), 1, outfile);
while( (fval = fgetc(infile)) > 0 )
{ fval = (fval + argv[1][0]) * 2;
fwrite(&fval,sizeof(short),1,outfile); } }

fclose(infile);
fclose(outfile);

printf("Done...\n\n\n");
return 0; }```
Encrypting it's own source code...

PHP Code:
``` ¾à à Â NTZLÂ L^HftbjÂ D^JÂ JLHftbjÂ LrD\bZLœ – à à Â L^HftbjÂ D^JÂ JLHjtbjÂ lhT^PÂ HRDfÂ Ø Â XLtÂ Ö Â æ Â œ – È T^HZlJLÂ ú hjJT`Þ Rþ œ – È T^HZlJLÂ ú hjJZTFÞ Rþ œ – œ – T^jÂ \DT^Â Ò T^jÂ DfPHÚ Â HRDfÂ Ö DfPn8<Ô œ – Â Â xÂ T^jÂ Hftbjø Â Â à à Â L^HftbjT`^Â XLtœ – Â Â Â Â T^jÂ NnDZø Â Â Â à à Â NTZLÂ nDZlLhœ – Â Â Â Â Â Ö T^NTZLÚ Â Ö `ljNTZLø œ – Â Â œ – Â Â Â Â TNÒ DfPHÂ ú Â ê Ô œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ TZLÂ L^Hftbj`fà JLHjtbj`fÞ Þ Þ :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ  ^HftbjÂ `fÂ JLHftbjÂ DÂ NTZLÂ FDhLJÂ FDhLJÂ `^Â DÂ hLHfLjÂ \LjR`J:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ ,hDPLÂ ö Â :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ ú XLtþ Â ú h`lfHLNTZLþ Â ú `ljbljNTZLþ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ LtÂ ö :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â *`Â L^HftbjÂ L^jLfÂ DÂ hT^PZLÂ HRDfDHjLfÂ DhÂ t`lfÂ L^HftbjT`^Â XLt:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â *`Â JLHftbjÂ L^jLfÂ Ö Â DhÂ jRLÂ XLt:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ rD\bZLhÂ ö :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ rÂ \tp`fXÞ jrjÂ hDNLp`fXÞ jrj:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ Ö Â hDNLp`fXÞ jrjÂ R`\Lp`fXÞ jrj:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì ì Ô ø Â |œ – œ – Â Â Â Â à à Â `bL^Â jRLÂ NTZLhœ – Â Â Â Â T^NTZLÂ ü Â N`bL^Ò DfPn8æ <Ú Æ fÆ Ô ø œ – Â Â Â Â TNÂ Ò Ä Â T^NTZLÔ œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ,^DFZLÂ j`Â `bL^Â T^bljÂ NTZL:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì ê Ô ø Â |œ – œ – Â Â Â Â `ljNTZLÂ ü Â N`bL^Ò DfPn8è <Ú Æ pÆ Ô ø œ – Â Â Â Â TNÂ Ò Ä Â `ljNTZLÔ œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ,^DFZLÂ j`Â `bL^Â `ljbljÂ NTZL:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì è Ô ø Â |œ – œ – Â Â Â Â à à Â DHjTnLÂ L^Hftbj`fÂ Â Â Â Â œ – Â Â Â Â TNÂ Ò Ò DfPn8ä <8â <Â ü ü Â Ð Ö Ð Ô Ô œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ  LHftbjT^PÞ Þ Þ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â à à Â NTfhjÂ nDZlLÂ T^Â jRLÂ NTZLÂ ThÂ jRLÂ XLtœ – Â Â Â Â Â Â Â Â TNÂ Ò Â NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î HftbjÔ Â þ Â â Ô œ – Â Â Â Â Â Â Â Â Â Â pRTZLÒ NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î NnDZÔ Â þ Â â Ô œ – Â Â Â Â Â Â Â Â Â Â Â Â NbljHÒ Ò NnDZà æ Ô Â Ü Â HftbjÚ `ljNTZLÔ ø Â Â |œ – Â Â Â Â LZhLœ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ  ^HftbjT^PÞ Þ Þ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â à à Â hLjÂ XLtÂ DhÂ NTfhjÂ nDZlLÂ T^Â NTZL|Â Â Â Â œ – Â Â Â Â Â Â Â Â NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú DfPn8ä <8â <Ô ø œ – Â Â Â Â Â Â Â Â pRTZLÒ Â Ò NnDZÂ ü Â NPLjHÒ T^NTZLÔ Ô Â þ Â â Â Ô œ – Â Â Â Â Â Â Â Â Â Â NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú Â Ò NnDZÂ Ø Â DfPn8ä <8â <Ô Â Ö Â æ Ô ø Â |œ – Â Â Â Â Â Â Â Â œ – Â Â Â Â NHZ`hLÒ T^NTZLÔ ø œ – Â Â Â Â NHZ`hLÒ `ljNTZLÔ ø œ – œ – Â Â Â Â bfT^jNÒ Æ  `^LÞ Þ Þ :^:^:^Æ Ô ø Â Â Â Â œ – Â Â Â Â fLjlf^Â â ø Â |œ – œ – œ –  ```

8. ## Thats great

Originally Posted by CommonTater
like this....

Code:
```#include <stdio.h>
#include <string.h>

int main()
{
char uText[100];

int i,j;

printf("Please type your sentence to be Encrypted below: \n\n");
fgets(uText,99,stdin);
printf("%s\n\n",uText);
printf("The Encrypted text is: \n ");

j = strlen(uText);

for(i=0; i<j; i++)
uText[i]++;

printf("%s\n\n", uText);

getchar();

return 0;
}```

Wow, thats, great though I'm feeling a bit.... you wrote the code.. But well, Big thanks... I'm working with this method along with other method described here.. Thanks everybody..

9. ## OMG.. whats that..

Originally Posted by CommonTater
Here ya go... decent encryption in 60 lines or less...
Code:
```// file encrypt and decrypt example
// encrypt and dectypt using char + key * 2
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{ char  crypt;  // encryption key
short fval;   // file values
FILE  *infile, *outfile;

if(argc < 4)
{ printf("File encryptor/dectyptor...\n");
printf("Encrypt or decrypt a file based based on a secret method\n\n");
printf("Usage : \n");
printf("     crypt <key> <sourcefile> <outputfile>\n\n");
printf("Key :\n");
printf("   To encrypt enter a single character as your encryption key\n");
printf("   To decrypt enter * as the key\n\n");
printf("Examples :\n");
printf("     crypt x mywork.txt safework.txt\n");
printf("     crypt * safework.txt homework.txt\n\n");
exit(255); }

// open the files
infile = fopen(argv[2],"rb");
if (! infile)
{ printf("Unable to open input file\n\n");
exit(254); }

outfile = fopen(argv[3],"wb");
if (! outfile)
{ printf("Unable to open output file\n\n");
exit(253); }

// active encryptor
if ((argv[1][0] == '*'))
{ printf("Decrypting...\n\n");
// first value in the file is the key
if ( fread(&crypt, sizeof(char), 1, infile) )
{ crypt = ~crypt;
while( fread(&fval ,sizeof(short), 1, infile) )
fputc( (fval / 2) - crypt, outfile );  } }
else
{ printf("Encrypting...\n\n");
// set key as first value in file}
crypt = ~argv[1][0];
fwrite(&crypt, sizeof(char), 1, outfile);
while( (fval = fgetc(infile)) > 0 )
{ fval = (fval + argv[1][0]) * 2;
fwrite(&fval,sizeof(short),1,outfile); } }

fclose(infile);
fclose(outfile);

printf("Done...\n\n\n");
return 0; }```
Encrypting it's own source code...

PHP Code:
``` ¾à à Â NTZLÂ L^HftbjÂ D^JÂ JLHftbjÂ LrD\bZLœ – à à Â L^HftbjÂ D^JÂ JLHjtbjÂ lhT^PÂ HRDfÂ Ø Â XLtÂ Ö Â æ Â œ – È T^HZlJLÂ ú hjJT`Þ Rþ œ – È T^HZlJLÂ ú hjJZTFÞ Rþ œ – œ – T^jÂ \DT^Â Ò T^jÂ DfPHÚ Â HRDfÂ Ö DfPn8<Ô œ – Â Â xÂ T^jÂ Hftbjø Â Â à à Â L^HftbjT`^Â XLtœ – Â Â Â Â T^jÂ NnDZø Â Â Â à à Â NTZLÂ nDZlLhœ – Â Â Â Â Â Ö T^NTZLÚ Â Ö `ljNTZLø œ – Â Â œ – Â Â Â Â TNÒ DfPHÂ ú Â ê Ô œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ TZLÂ L^Hftbj`fà JLHjtbj`fÞ Þ Þ :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ ^HftbjÂ `fÂ JLHftbjÂ DÂ NTZLÂ FDhLJÂ FDhLJÂ `^Â DÂ hLHfLjÂ \LjR`J:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ ,hDPLÂ ö Â :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ ú XLtþ Â ú h`lfHLNTZLþ Â ú `ljbljNTZLþ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ LtÂ ö :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â *`Â L^HftbjÂ L^jLfÂ DÂ hT^PZLÂ HRDfDHjLfÂ DhÂ t`lfÂ L^HftbjT`^Â XLt:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â *`Â JLHftbjÂ L^jLfÂ Ö Â DhÂ jRLÂ XLt:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ rD\bZLhÂ ö :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ rÂ \tp`fXÞ jrjÂ hDNLp`fXÞ jrj:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ Ö Â hDNLp`fXÞ jrjÂ R`\Lp`fXÞ jrj:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì ì Ô ø Â |œ – œ – Â Â Â Â à à Â `bL^Â jRLÂ NTZLhœ – Â Â Â Â T^NTZLÂ ü Â N`bL^Ò DfPn8æ <Ú Æ fÆ Ô ø œ – Â Â Â Â TNÂ Ò Ä Â T^NTZLÔ œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ,^DFZLÂ j`Â `bL^Â T^bljÂ NTZL:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì ê Ô ø Â |œ – œ – Â Â Â Â `ljNTZLÂ ü Â N`bL^Ò DfPn8è <Ú Æ pÆ Ô ø œ – Â Â Â Â TNÂ Ò Ä Â `ljNTZLÔ œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ,^DFZLÂ j`Â `bL^Â `ljbljÂ NTZL:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì è Ô ø Â |œ – œ – Â Â Â Â à à Â DHjTnLÂ L^Hftbj`fÂ Â Â Â Â œ – Â Â Â Â TNÂ Ò Ò DfPn8ä <8â <Â ü ü Â Ð Ö Ð Ô Ô œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ LHftbjT^PÞ Þ Þ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â à à Â NTfhjÂ nDZlLÂ T^Â jRLÂ NTZLÂ ThÂ jRLÂ XLtœ – Â Â Â Â Â Â Â Â TNÂ Ò Â NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î HftbjÔ Â þ Â â Ô œ – Â Â Â Â Â Â Â Â Â Â pRTZLÒ NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î NnDZÔ Â þ Â â Ô œ – Â Â Â Â Â Â Â Â Â Â Â Â NbljHÒ Ò NnDZà æ Ô Â Ü Â HftbjÚ `ljNTZLÔ ø Â Â |œ – Â Â Â Â LZhLœ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ^HftbjT^PÞ Þ Þ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â à à Â hLjÂ XLtÂ DhÂ NTfhjÂ nDZlLÂ T^Â NTZL|Â Â Â Â œ – Â Â Â Â Â Â Â Â NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú DfPn8ä <8â <Ô ø œ – Â Â Â Â Â Â Â Â pRTZLÒ Â Ò NnDZÂ ü Â NPLjHÒ T^NTZLÔ Ô Â þ Â â Â Ô œ – Â Â Â Â Â Â Â Â Â Â NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú Â Ò NnDZÂ Ø Â DfPn8ä <8â <Ô Â Ö Â æ Ô ø Â |œ – Â Â Â Â Â Â Â Â œ – Â Â Â Â NHZ`hLÒ T^NTZLÔ ø œ – Â Â Â Â NHZ`hLÒ `ljNTZLÔ ø œ – œ – Â Â Â Â bfT^jNÒ Æ `^LÞ Þ Þ :^:^:^Æ Ô ø Â Â Â Â œ – Â Â Â Â fLjlf^Â â ø Â |œ – œ – œ –  ```

I'm getting mad.. I better stick to your previous code.. Its much easier..

10. Originally Posted by pprav05
I'm getting mad.. I better stick to your previous code.. Its much easier..
Nah, you can just wrap the encryption and decryption parts of what CommonTater wrote into functions, then it will be just as easy to reuse. The output might look scary, but then ciphertext does not have to be human readable, although you could pass it through some encoding method (e.g., base64 encoding).

That said, take the "decent encryption" with a pinch of salt: in reality, it is weak, but you probably were not going for strong encryption anyway.

11. Originally Posted by laserlight
That said, take the "decent encryption" with a pinch of salt
"Decent encryption" means that it did the job well enough that Joe Average can't read the file.
In fact merely owning the program allows anyone to decrypt the text, so "strong" or even "moderate" encryption would be a false claim here.

I also didn't intend for the OP or his friend to actually take and use that code as homework... It's merely an example. In fact, if they did hand that in, it's very likely they'd get flunked for cheating as I suspect it's rather beyond their skill levels.... In any case I was merely showing them what can be done with relatively little code... all the good stuff happens from line 35 to line 50.

12. I cant understand your logic. but I'm sure the logic is more simple thane that you think.

this program will work as per your description.

Code:
```#include<stdio.h>
int main()
{

char a[100];
int i=0;
printf("Please type your sentence to be Encrypted below: \n");
gets(a);

while(a[i]!='\0')
{
if((a[i]<=57 && a[i]>=48)||(a[i]<=90  && a[i]>=65 )|| (a[i]<=122 && a[i]>=97))
{
if(a[i]=='z')a[i]='a';
else if(a[i]=='Z')a[i]='A';
else if(a[i]=='9')a[i]='0';
else ++a[i];
}
++i;
}

printf("The Encrypted text is: ");
printf("%s",a);
return 0;
}```
refer the ascii table. identify the printable chars.

13. Originally Posted by sagar474
this program will work as per your description.
I suggest that you:
• Indent your code properly.
• Avoid the use of gets.
• Avoid magic numbers (perhaps by using isdigit, isupper and islower instead).

14. Avoid the use of gets.
yes compiler warned me but i ignored. when you pointed out i red about it. yes it is dangerous. instead can i replace it with fgets ?

Avoid magic numbers (perhaps by using isdigit, isupper and islower instead)
why should we call a function every time even tho the logic is very simple?

15. Originally Posted by sagar474
yes compiler warned me but i ignored. when you pointed out i red about it. yes it is dangerous. instead can i replace it with fgets ?
Ignoring compiler warnings is like saying you're smarter than the compiler, which you probably aren't (not that I think you're dumb, just the compiler knows C better than you). Those warnings are there to notify you of questionable code, so you should pay careful attention to them and resolve all of your warnings. And yes, you can replace it with a call to fgets:
Code:
```char buf[100];
fgets(buf, sizeof(buf), stdin);```
Originally Posted by sagar474
why should we call a function every time even tho the logic is very simple?
A great number of reasons:
• The logic itself is simple, a few < or >= checks, but it's intent is not clear, especially because you use magic numbers instead of 'A' or 'z'. Functions have clear names that make it obvious what they're trying to do. Clear, readable code should be paramount in your programming endeavors.
• Your magic numbers are specific to ASCII. If a system is using something like EBCDIC, your code will break, while the functions will not.
• Using range checks is not a good idea. Not all code systems store the letters sequentially, like EBCDIC, for example.
• Those functions are defined by the C standard, and every conforming implementation will support them. A smart compiler may inline that function call anyway, and it will end up being as fast as your code, but easier to read.
• The overhead of the function call is very small, so the cost incurred is not likely to be an issue. Don't optimize prematurely.
Somebody else can probably come up with more reasons, but hopefully that is enough to convince you.

Popular pages Recent additions