Thread: Help printing strings

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Help printing strings

    Hey guys!

    I've been working on a program that will create a randomly generated password using the numbers 0-9 and the letters A-Z all in uppercase. I have my program created and have to come to my final impass before I complete this program.

    I have been asked by my teacher that this has to be printed with the %s specifier. In the code below, it comes out weird. If I switch the array password[8] to a char from an int, it does a weird response as well. Since this is homework and I want to learn how to do this correctly, if you could possibly give me a hint as to what to do to solve this, that would be wicked. If you want to give the anwser, that is cool as well.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void numbers12(int password[8]);
    void characters34(int password[8]);
    void numbers56(int password[8]);
    void characters78(int password[8]);
    
    
    int main ()
    {
    	srand(time(NULL));
    	int password[8];
    
    
    	numbers12(password);
    	characters34(password);
    	numbers56(password);
    	characters78(password);
    
    
    	printf("Your randomly generated password comprised of numbers and letters is %s.\n", password);
    
    
    	return 0;
    }
    
    
    void numbers12(int password[8])
    {
    	int *ptr;
    
    
    	ptr = &password[0];
    
    
    	for(int i = 0; i < 2; i++)
    	{
    		ptr[i] = rand() % 10;
    		if(ptr[1] == ptr[0])
    		{
    			while(ptr[1] == ptr[0])
    			{
    				ptr[1] = rand() % 10;
    				break;
    			}
    		}
    		printf("A part of your password is the number %d.\n", ptr[i]);
    	}
    }
    
    
    void characters34(int password[8])
    {
    	int *ptr;
    
    
    	ptr = &password[0];
    
    
    	for(int i = 2; i < 4; i++)
    	{
    		ptr[i] = (rand() % 26) + 'A';
    		if(ptr[3] == ptr[2])
    		{
    			while(ptr[3] == ptr[2])
    			{
    				ptr[3] = (rand() % 26) +'A';
    				break;
    			}
    		}
    		printf("A part of your password is the character %c.\n", ptr[i]);
    	}
    }
    
    
    void numbers56(int password[8])
    {
    	int *ptr;
    
    
    	ptr = &password[0];
    
    
    	for(int i = 4; i < 6; i++)
    	{
    		ptr[i] = rand() % 10;
    		if(ptr[4] == ptr[0] || ptr[1])
    		{
    			while(ptr[4] == ptr[0] || ptr[1])
    			{
    				ptr[4] = rand() % 10;
    				break;
    			}
    		}
    		if(ptr[5] == ptr[0] || ptr[1] || ptr[4])
    		{
    			while(ptr[5] == ptr[0] || ptr[1] || ptr[4])
    			{
    				ptr[5] = rand() % 10;
    				break;
    			}
    		}
    		printf("A part of your password is the number %d.\n", ptr[i]);
    	}
    }
    
    
    void characters78(int password[8])
    {
    	int *ptr;
    
    
    	ptr = &password[0];
    
    
    	for(int i = 6; i < 8; i++)
    	{
    		ptr[i] = (rand() % 26) + 'A';
    		if(ptr[6] == ptr[2] || ptr[3])
    		{
    			while(ptr[6] == ptr[2] || ptr[3])
    			{
    				ptr[6] = (rand() % 26) +'A';
    				break;
    			}
    		}
    		if(ptr[7] == ptr[2] || ptr[3] || ptr[6])
    		{
    			while(ptr[7] == ptr[2] || ptr[3] || ptr[6])
    			{
    				ptr[7] = (rand() % 26) + 'A';
    				break;
    			}
    		}
    		printf("A part of your password is the character %c.\n", ptr[i]);
    	}
    }
    Thanks again =D
    -Frendin

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    I would also like to specify that those while loops are to prevent repeats of the numbers. There are supposed to be no repeated numbers or letters in the password. I am working on fixing that.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Strings have to be char* (or arrays of char) and they have to be null-terminated. So, my suggestion:

    1. switch back to char password[]
    2. make it one more character long (e.g. char password[9])
    3. make sure the last character is a '\0', e.g. set password[8] = '\0'

    Then when you do printf("%s\n", password) it will make sure that password is printed correctly

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This
    Code:
    while(ptr[7] == ptr[2] || ptr[3] || ptr[6])
    is equivalent to
    Code:
    while((ptr[7] == ptr[2]) || (ptr[3] != 0) || (ptr[6] != 0))
    instead of what you probably meant, i.e.
    Code:
    while((ptr[7] == ptr[2]) || (ptr[7] == ptr[3]) || (ptr[7] == ptr[6]))
    You have a lot of repeated code, and a lot of code that references constants, like that bit I quoted -- why are you saying ptr[7]? You should rarely mention array indices like that directly. A lot of it is unnecessary too. Like this.
    Code:
            if(ptr[3] == ptr[2])
            {
                while(ptr[3] == ptr[2])
                {
                    ptr[3] = (rand() % 26) +'A';
                    break;
                }
            }
    I'll assume you didn't really mean to have the break in since it renders the while loop irrelevant. In that case, here's some equivalent code.
    Code:
    do {
        ptr[3] = rand() % 26 + 'A';
    } while(ptr[3] == ptr[2]);
    You should probably have a function for that. Maybe a function that takes in an array of letters you've already generated, so that it can iterate through the array and check for duplicates. That way you'd catch all repeats rather than just ptr[3] == ptr[2]. Example:
    Code:
    char random_letter_that_hasnt_been_generated_yet(char generated[], int generated_count) {
        for(;;) {
            char c = rand() % 26 + 'A';
            int i, already_generated = 0;
            for(i = 0; i < generated_count && !already_generated; i ++) {
                if(c == generated[i]) already_generated = 1;
            }
            if(!already_generated) {
                return c;
            }
        }
    }
    Of course it may be a good idea to not make that loop infinite, in case you try to generate 27 distinct letters (with only 26 different possibilities). The pigeonhole principle strikes again.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Thanks for the help guys. My code is still coming up weird c99tutorial, so I will have to take a longer look at it. Thank you for the lesson dwks, I'll take a quick break from my code and then see if I can't change those while loops. I also don't know what those do functions do so I will look those up as well.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    printf("Your randomly generated password comprised of numbers and letters is %s.\n", password);
    
    
    ...
    
    
    ptr[i] = rand() % 10;

    Your numbers are not in ascii format.


    You can do one of two things:
    Add: rand() % 10 + '0'

    OR


    Change the printf at the end to print an number, number, character, character, ...




    If I were you, I'd go through and change all the inputs to include + '0'
    Fact - Beethoven wrote his first symphony in C

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, c99tutorial's advice needs to be taken with the string being of type char instead of int for the printf
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing out strings
    By mouse666666 in forum C Programming
    Replies: 2
    Last Post: 02-08-2010, 12:27 AM
  2. printing strings
    By Tom_Arch in forum C Programming
    Replies: 4
    Last Post: 04-25-2009, 03:23 AM
  3. printing wide characters to strings...
    By davo666 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 12:56 AM
  4. printing char* strings
    By difficult.name in forum C Programming
    Replies: 4
    Last Post: 12-10-2004, 07:06 PM
  5. printing an array of strings
    By linucksrox in forum C Programming
    Replies: 3
    Last Post: 05-11-2004, 03:31 PM