HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

String scrambler written in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
writtenstringscrambler

Problem

I've created a basic password scrambler that can run a single string or a file. It will take your character, find the ascii code value of it, floor it, divide it by 16, grab whatever that equals out of the cipher array, and replace it with that character. For example:

e@ubuntu:~/bin/c$ ./ciph.exe -h
Usage: ciph -[s|f|t|h]
-s Run a single string
-f Run a file
-t Run the test strings
-h Run this help and exit
e@ubuntu:~/bin/c$ ./ciph.exe -s "orange dog"
Original: orange dog
Scrambled: bcjxnfzgbn
ekultek@ubuntu:~/bin/c$ ./ciph.exe -s "purple panda"
Original: purple panda
Scrambled: jqcjzfzjjxgj
e@ubuntu:~/bin/c$ ./ciph.exe -f test.txt
Retreived line testing
Scrambled: ffgf
Retreived line Another test
Scrambled: bxbfmfczffgf
Retreived line One more test
Scrambled: hxfzhbcfzffgf
Retreived line this is the final test
Scrambled: fm
e@ubuntu:~/bin/c$ ./ciph.exe -t
Running test strings...

Original: Adm1n1strat0r
Scrambled: bghxxxgfcjfhc
Original: TeSt
Scrambled: cfvf
Original: Ch1c4g0 Bu115!
Scrambled: jmxcjnhzpqxxvh


This is the first every program that I've written in C, and I would like some critique on my work, things I'd like to focus on (obviously critique the whole thing):

  • Are there better ways to parse command line arguments?



  • Is there a better way to strip a string of a new line?



  • Running through a file line by line, did I do it correctly?



```
#include
#include
#include

#define MAX 100

void helpPage(void)
{
/ *
* Help Page
*/

puts("Usage: ciph -[s|f|t|h]");
puts("-s Run a single string");
puts("-f Run a file");
puts("-t Run the test strings");
puts("-h Run this help and exit");
}

char strip(char s)
{
/ *
* Strip a string of a new line
* Example:
* strip("test\n");
* test
*/

return strtok(s, "\n");
}

static char scramblePassword (char pwd, char *result)
{
/
* Create a scrambled password
* Example:
*
* scramblePasswo

Solution

For a very first program, not bad at all. Issues:

-
"The file does not exist" error message could be misleading. There is a number of reasons why fopen() may fail (for example, insufficient permissions), so it is better to leave it to the system. Check out perror().

-
For consistency, replace "Retrieved line" with "Original".

-
Comments line / Run the test strings / are to be avoided. If you feel that the purpose of a particular code is unclear, factor it out to a properly named function (just like you did with strip). For example,

case 'f':
       scramble_lines_from_file(argv[2]);
       break;


-
Test argv[2] before using it.

-
I don't see why scramblePassword should return result;: the result is already known to the caller.

Code Snippets

case 'f':
       scramble_lines_from_file(argv[2]);
       break;

Context

StackExchange Code Review Q#159472, answer score: 3

Revisions (0)

No revisions yet.