Jump to content

Hi guys. I really need help here. I have school task which i have to make for my final grade. It is C program for password generation. Can someone help me with it? I know i am asking too much, but i have no other choice. I am really bad at C. I will really appreciate your help. Some advices are wellcomed too. So here is the task itself:

 

Create password generator, which uses dictionary and match files to generate passwords by replacing letters in the dictionary words with their matches.

Example:

Dictionary file:

apple

loop

Match file:

a 4

e 3

o 0

Output:

4pple

appl3

4ppl3

l0op

lo0p

l00p 

 

Thanks in advance to everyone willing to help. I will be extremely grateful. 

Sorry if my English is not perfect, but it isn't my native language :)

 

Link to comment
https://linustechtips.com/topic/13811-c-program-help/
Share on other sites

Link to post
Share on other sites

Guys, there's code tags here! :lol:

Anyway, I'm curious about this, I will take a look.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-150455
Share on other sites

Link to post
Share on other sites

First you need to look at functions that allow you to open files and to read and write to them.

Then you could iterate over the matches file and, for each match, insert the original char into one array and the corresponding matching char into another array but at the same position.

Then you would iterate over the dictionary file and, for each word, for each character, see if there is an entry in your array of original chars. If there is get the match from the other array at the same position you found the original, if not move on. Then you would need to store the password somewhere (another file or print it to the console).

 

This is the basic skeleton of the solution. Some details may require attention (like how do you know how many positions you need in your array), but you could start with this.

Hope it helps.

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-150565
Share on other sites

Link to post
Share on other sites

This is not the final solution!

I'm not really for just completely doing anyone's homework ;)

But, this should give you a pretty good idea of how to proceed. This program takes

an input parameter from the console:

% programname "This is a test string."
And replaces the specified characters in its argument, for example:

 

Your initial string: "This is a test string."Your new string: "THIS IS A TEST STRING."
The necessary shift characters are stored in "shift.txt" (place that in the

same directory as your program).

Actual program code

#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char *argv[]){    if(!argv[1])	{		printf("No arguments given, can do nothing. Exiting.\n");		return 0;	}	printf("Your initial string: \"%s\"\n",argv[1]);	int length = strlen(argv[1]);	/* Open shift file. */	FILE *file_pointer;	char i,j;	file_pointer=fopen("shift.txt", "r");	/* Exit gracefully if shift.txt cannot be found. */	if (file_pointer == NULL) 	{		 printf("Couldn't open shift.txt for reading. Exiting.\n");		 return 0;	}	int k;	/* Go through the shift.txt file. */	while (fscanf(file_pointer, "%c %c\n", &i, &j)==2)	{		/* This loop actually does the replacing. */		for (k=0;k<length;k++)		{			if (argv[1][k]==i) 			{				argv[1][k]=j;			}		}	}	int fclose(FILE *file_pointer);	printf("Your new string: %s\n",argv[1]);	return 0;}
And the shift file will look as you speficied, for example:

 

a Ab Bc Cd De Ef Fg Gand so on and so fort...
Almost everything you need to do is in here, you just need to tweak it somewhat

and adjust it so that it does what you need. For reading in the file with the

original words, you can use something like:

 

#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){	FILE *file_pointer;	file_pointer=fopen("read.txt", "r");	/* Exit gracefully if shift.txt cannot be found. */	if (file_pointer == NULL) 	{		 printf("Couldn't open shift.txt for reading. Exiting.\n");		 return 0;	}	char *i;	char line[128];	/* Go through the shift.txt file. */	while (fgets(line,sizeof line,file_pointer) != NULL)	{		printf("%s",line);	}	int fclose(FILE *file_pointer);	return 0;}
And the "read.txt" file for that would look like:

appleorangechairetc.
It's been a while since I've done C, so this might not be up to the highest

standard, but it should get you started and allow you to piece together what

you need.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-150715
Share on other sites

Link to post
Share on other sites

Thank you very much. It looks very helpful. It is 1am in Bulgaria now so i will try it out tomorrow morning. I will post what i have done and may ask some questions :). Once again, thank you.

Sorry if my English is not perfect, but it isn't my native language :)

 

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-150801
Share on other sites

Link to post
Share on other sites

Thank you very much. It looks very helpful. It is 1am in Bulgaria now so i will try it out tomorrow morning. I will post what i have done and may ask some questions :). Once again, thank you.

Not a problem, we're only 1 hour behind you with regards to time zones, so just

check back when you need more help. :)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-150808
Share on other sites

Link to post
Share on other sites

Since this is a relevant update, I will allow myself a double post.

Anyway: I'm sorry, I couldn't sleep, and I got bored. :lol: Below is a rough draft of

my solution. Not great code and not perfect, but as far as I can tell it does what

it's supposed to do, and it has some basic error handling.

Usage:

% programname source_file shift_file output_file
Will create an output file and write to that.

Alternatively

% programname source_file shift_file
Will just print everything to the console without creating an output file.

 

#include <stdio.h>#include <string.h>/* function prototypes */int read_source(char* ,char* ,char* );char* shift_string(char* ,char* );int write_shifted(char* ,char* );int main(int argc, char *argv[]){	/* argv[1]: the source file with the original strings	 * argv[2]: the shift file which indicates which characters are to be	 * replaced with what	 * argv[3]: the file in which the new strings are to be stored*/	if(!argv[1])	{		printf("No source file, can do nothing. Exiting.\n");		return 1;	}	else if (!argv[2])	{		printf("No shift file given, can do nothing. Exiting.\n");		return 2;	}	else if (!argv[3])	{		/* If no output file has been specified, print the		 * output directly to the console. */		read_source(argv[1],argv[2],NULL);		return 0;	}	if (read_source(argv[1],argv[2],argv[3])!=0)	{		printf("There has been an error. Exiting.\n");		return 3;	}	if (argv[3])	{		printf("Everything seems to have gone according to plan.\n");		printf("Your output has been stored in \"%s\"\n",argv[3]);	}	return 0;}int read_source(char* source_file,char* shift_file,char* out_file){	FILE *file_pointer;	file_pointer=fopen(source_file, "r");	/* Exit gracefully if source_file cannot be found. */	if (file_pointer == NULL) 	{		 printf("Couldn't open \"%s\" for reading.\n",source_file);		 return 1;	}	char* new_line;        /* maximum line length, can be changed if needed */	char line[256];	/* Go through the source file. */	while (fgets(line,sizeof line,file_pointer) != NULL)	{		new_line = shift_string(shift_file,line);		if (new_line==NULL)		{			printf("There has been an error with replacing the characters.\n");			return 2;		}		write_shifted(new_line,out_file);	}	int fclose(FILE *file_pointer);	return 0;}char* shift_string(char* shift_file,char* source_string){	/* This function replaces certain characters in a given source_string as	 * specified by shift_file */	/* Open shift file. */	FILE *file_pointer;	char i,j;	file_pointer=fopen(shift_file, "r");	/* Exit gracefully if shift_file cannot be found. */	if (file_pointer == NULL) 	{		 printf("Couldn't open \"%s\" for reading.\n",shift_file);		 return NULL;	}	int k;	/* Determine how long the source_string is for the loop below. */	int length = strlen(source_string);	while (fscanf(file_pointer, "%c %c\n", &i, &j)==2)	{		/* This loop actually does the replacing. */		for (k=0;k<length;k++)		{			if (source_string[k]==i) 			{				source_string[k]=j;			}		}	}	int fclose(FILE *file_pointer);	return source_string;}int write_shifted(char* new_line,char* out_file){	/* This function writes the new strings to out_file	 * If no out_file has been given, it will write the	 * output to the console.*/	if (out_file==NULL)	{		printf("%s",new_line);		return 0;	}				FILE *file_pointer; 	/* Open in a+ mode. If out_file does not yet exist, it will be created.	 * If it does exist, it will be appended to instead of overwritten. */	file_pointer = fopen(out_file,"a+"); 	/* Write the new line */	fprintf(file_pointer,"%s",new_line);	fclose(file_pointer);	return 0; }
As far as I can understand this does what your first post asks. I still highly

encourage you to solve this yourself and then post your solution (I'll gladly help you

with that). The best way would be to write the program from scratch again.

But, as you can tell, I'm more of a night person and it will probably be a few hours

before I'm online again and can help out, so I thought I'd post this now in case you

get stuck. It should provide you with the basic outline, but there's lots of other

(and probably better ;)) ways to do this.

Happy coding :)

EDIT: source_file and shift_file should be formatted as in my previous post, of

course.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-151750
Share on other sites

Link to post
Share on other sites

Wow, i can't thank you enough :). Your code is very clear and i can try to solve this task by myself now. It is much more easy as i have already seen example solution.

First of all, i will try to change your replacing algorithm as now it only prints the final states (for example 4ppl3 l00p) but i also need the middle states (4pple appl3 l0op lo0p). It will be interesting to experiment till find the right solution. I am open to some advices though :). thanks again.

Sorry if my English is not perfect, but it isn't my native language :)

 

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-152487
Share on other sites

Link to post
Share on other sites

So i came up with a solution that do exactly what i need :). I have to mention that i won't be able to do it if it wasn't for your post alpenwasser. Thanks again. So please check the code:

#include <stdio.h>#include <string.h>int read_source(char* ,char*);char* shift_string(char* ,char* );int main(int argc, char *argv[]){        if(argc==3)        {            read_source(argv[1],argv[2]);            return 0;        }        else {            printf("Missing files.\n");            return 1;        }                if (read_source(argv[1],argv[2])!=0)        {            printf("There has been an error. Exiting.\n");            return 2;        }                return 0;}int read_source(char* source_file,char* shift_file){FILE *file_pointer;file_pointer=fopen(source_file, "r");if (file_pointer == NULL){        printf("Couldn't open \"%s\" for reading.\n",source_file);        return 1;}    char new_line[256];char line[256];char temp[256];char temp1[256];    while (fgets(line,sizeof line,file_pointer) !=NULL){int count=0;        int diff=0;        for(int k=0; k<256;k++){            if(line[k]=='\0'){                count=count-1;                break;            }            else count++;        }        strcpy(temp, line);        strcpy(new_line, shift_string(shift_file,line));        strcpy(temp1, new_line);        if (new_line==NULL){printf("There has been an error with replacing the characters.\n");return 2;}                for(int i=0;i<count+1;i++){            if(new_line[i]!=temp[i]){                diff++;            }        }                for(int m = 0;m<diff;m++){            int flag=0;            for(int i=0;i<count+1;i++)            {                if(temp1[i]!=temp[i] && flag==0){                    printf("%c", temp1[i]);                    temp1[i]=temp[i];                    flag=1;                }                else printf("%c", temp[i]);            }        }                if(diff!=1){            printf("%s\n",new_line);        }}int fclose(FILE *file_pointer);    return 0;}char* shift_string(char* shift_file,char* source_string){FILE *file_pointer;char i,j;file_pointer=fopen(shift_file, "r");if (file_pointer == NULL){        printf("Couldn't open \"%s\" for reading.\n",shift_file);        return NULL;}    int k;int length = (int) strlen(source_string);    while (fscanf(file_pointer, "%c %c\n", &i, &j)==2){        for (k=0;k<length;k++)            {                if (source_string[k]==i)                {                    source_string[k]=j;                }            }        }int fclose(FILE *file_pointer);    return source_string;}

 

But now i have different problem :D. the output is the following:

 

4pple

appl3

4ppl3

 

l0oplo0pl00p

 

But it should be:

 

 

4pple

appl3

4ppl3

 

l0op

lo0p

l00p

 

It is not a big deal (just some newlines) but i will be glad if someone can tell me where is my mistake. :)

Sorry if my English is not perfect, but it isn't my native language :)

 

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-155383
Share on other sites

Link to post
Share on other sites

Just had a test run, and everything works for me as far as I can tell (even the loop

replacement section). :)

EDIT: I'm assuming you are running Windows? If so, Windows text files are

different with regards to newline characters than UNIX text files (I'm running

Linux). Since I don't have a Windows machine available I can't test this, but maybe

try adding an additional newline at the end of the file.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-157673
Share on other sites

Link to post
Share on other sites

Just had a test run, and everything works for me as far as I can tell (even the loop

replacement section). :)

EDIT: I'm assuming you are running Windows? If so, Windows text files are

different with regards to newline characters than UNIX text files (I'm running

Linux). Since I don't have a Windows machine available I can't test this, but maybe

try adding an additional newline at the end of the file.

No, i am running Macintosh which is also UNIX-based. If i add newline at the end of the file, the output will be:

4pple

 

appl3

 

4ppl3

 

l0op

lo0p

l00p

 

This solves the problem but causes another. 

So this newline problem is only appearing at the last word but only if the word has more than one letters for replacement (for example "loop" which has 2 o's, if the last word was "baby" (only one letter to replace "a") it is ok. 

Sorry if my English is not perfect, but it isn't my native language :)

 

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-157988
Share on other sites

Link to post
Share on other sites

Hm, that is interesting. No matter what I do, I cannot cause that problem to appear

on my machine. I can even add arbitrary empty lines anywhere and it still works

without problems (the empty lines are added to the output, but not between different

versions of the same word, only between different words).

But when I have a word in which only one character is replaced, there is no empty

line between that word and the next one:

@ppleappl3@ppl3b@byl0oplo0pl00p
Very curious, this behavior. :lol:

I'll keep looking, I'm sure there's a solution to this.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-158236
Share on other sites

Link to post
Share on other sites

I'm not entirely sure if this solves your problem, but it solves mine about baby

not having a line break after it.

Of course this will probably not solve your problem, but it's at least something. ;)

LINE NUMBERS: I just noticed that you can enable line numbers by changing

the text inside your code tags to code=auto:1. Makes discussing things a bit easier :)

My output now:

@ppleappl3@ppl3b@byl0oplo0pl00p
The thing that changed this for me was adding

else{        printf("\n");}
after

if(diff!=1){        printf("%s\n",new_line);}
My new code (I've also changed the indentation a bit, but that's just style and not

really relevant for functionality, I'm just a bit of a neat freak when it comes

to my code ;)):

#include <stdio.h>#include <string.h>int read_source(char* ,char*);char* shift_string(char* ,char* );int main(int argc, char *argv[]){        if(argc==3)        {            read_source(argv[1],argv[2]);            return 0;        }        else {            printf("Missing files.\n");            return 1;        }                if (read_source(argv[1],argv[2])!=0)        {            printf("There has been an error. Exiting.\n");            return 2;        }                return 0;}int read_source(char* source_file,char* shift_file){	FILE *file_pointer;	file_pointer=fopen(source_file, "r");	if (file_pointer == NULL)	{		printf("Couldn't open \"%s\" for reading.\n",source_file);		return 1;	}			char new_line[256];	char line[256];	char temp[256];	char temp1[256];			while (fgets(line,sizeof line,file_pointer) !=NULL)	{		int count=0;		int diff=0;		for(int k=0; sizeof line;k++)		{			if(line[k]=='\0')			{				count=count-1;				break;			}			else			{				count++;			}		}		strcpy(temp, line);		strcpy(new_line, shift_string(shift_file,line));		strcpy(temp1, new_line);						if (new_line==NULL)		{			printf("There has been an error with replacing the characters.\n");			return 2;		}					for(int i=0;i<count+1;i++)		{			if(new_line[i]!=temp[i])			{				diff++;			}		}					for(int m = 0;m<diff;m++)		{			int flag=0;			for(int i=0;i<count+1;i++)			{				if(temp1[i]!=temp[i] && flag==0)				{					printf("%c", temp1[i]);					temp1[i]=temp[i];					flag=1;				}				else printf("%c", temp[i]);			}		}					if(diff!=1)		{			printf("%s\n",new_line);		}		else		{			printf("\n");		}	}	int fclose(FILE *file_pointer);			return 0;}char* shift_string(char* shift_file,char* source_string){	FILE *file_pointer;	char i,j;	file_pointer=fopen(shift_file, "r");	if (file_pointer == NULL)	{		printf("Couldn't open \"%s\" for reading.\n",shift_file);		return NULL;	}			int k;	int length = (int) strlen(source_string);			while (fscanf(file_pointer, "%c %c\n", &i, &j)==2)	{		for (k=0;k<length;k++)		{		        if (source_string[k]==i)			{				source_string[k]=j;			}		}	}	int fclose(FILE *file_pointer);			return source_string;}
EDIT: Yes, double post. It's a relevant update to the thread and therefore

justifiable imho. ;)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-158322
Share on other sites

Link to post
Share on other sites

I found out why you can't see the problem. :)

My source file is:

appleloop 

But if i make it:

appleloop

there will be no newline problem. Note that the only difference is that there is one additional blank line at the end (after you write "loop" just press enter so the cursor jumps down a row and then save the file). Check it to see if i am right. :)

 

EDIT: yes, your additional "else" statement really solves the "baby" problem. I haven't even seen this problem. Thanks for pointing it out to me.   

Sorry if my English is not perfect, but it isn't my native language :)

 

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-158542
Share on other sites

Link to post
Share on other sites

I found out why you can't see the problem. :)

My source file is:

appleloop
But if i make it:
appleloop
there will be no newline problem. Note that the only difference is that there is one additional blank line at the end (after you write "loop" just press enter so the cursor jumps down a row and then save the file). Check it to see if i am right. :)

It doesn't make any difference on my system actually, but that is exactly what I meant above! :lol:

Sorry about that, I should have made an example to better explain. But I'm glad it's working now. :)

I'm thinking this has probably to do with fgets. Maybe that's just implemented slightly differently

between our platforms.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/13811-c-program-help/#findComment-158747
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×