Thread: Splittingstring into array characters

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Splittingstring into array characters

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <string.h>
    
    int main()
    {
    int n = 8;
    int i = 0;
    char* password[9] = {"p","a","s","s","w","o","r","d"};
    char typePassword[9]; 
    int lenghtPSW;
    
    scanf("%s", typePassword);
        while(i < n)
        {
           printf("%s\n",password[i]);
           i++;
        }
        printf("%s\n", typePassword);
        lenghtPSW = strlen(typePassword);
        printf("%d\n", lenghtPSW);
        
        system("Pause");
        
        if(lenghtPSW == n)
        {
                char ArrayPswd[lenghtPSW];
                printf("Continue\n");
                while(n > 0)
                {
                strcpy(ArrayPswd, typePassword);
                n--;
                printf("%s\n", ArrayPswd);
                i++;    
                }
        }
    }
    Hello everyone,
    could someone help me figure out how use strcpy function to copy each leter to a string and not whole word password, to make it print
    p
    a
    s
    s
    w
    o
    r
    d
    and not 8 password words, new in programing just trying to understand it better.
    Thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I'm sorry but I can't understand what you're trying to do. If you just wanted to print all of the strings, you could just do:
    Code:
    for (i = 0; i < 9; i++) {
        puts(password[i]);
    }
    or if you wanted to copy the characters:
    Code:
    /* This is how it should be declared in order to be able to change the values */
    char password[9][2] = {"p","a","s","s","w","o","r","d"}; 
    
    for (i = 0; i < 9; i++) {
        password[i][0] = typePassword[i];
    }
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    replay

    I am trying to copy typePassword - string, the scanf string i have typed in to console to arrayPswd, to make arrayPswd look smth like this
    Code:
    arrayPswd[lenghtPSW] = {"p","a","s","s","w","o","r","d"};
    and then i would like to print it
    arrayPswd[0] = p,
    arrayPsw[1] = a,
    and so on.
    Code:
    if(lenghtPSW == n)
        {
                char ArrayPswd[lenghtPSW];
                printf("Continue\n");
                while(n > 0)
                {
                strcpy(ArrayPswd, typePassword);
                n--;
                printf("%s\n", ArrayPswd);
                i++;    
                }
        }
    i am talking about this part.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Oh, I get it now. There are two ways you can do it( that I can think of ):
    #1: Have an array of strings, just like "password"
    Code:
    char ArrayPswd[lenghtPSW][2] = { 0 }; /* Initializes all the bytes to zero */
    
    for(...) {
        ArrayPswd[i][0] = typePassword[i];
    }
    #2: Have a single array and print it differently
    Code:
    char ArrayPswd[lenghtPSW];
    
    strncpy(ArrayPswd, typePassword, lenghtPSW);
    for (...) {
        printf("%c\n", ArrayPswd[i]);
    }
    The above code isn't runnable, but you can modify it to suit your needs.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. characters array
    By kinois in forum C Programming
    Replies: 6
    Last Post: 03-21-2014, 09:29 PM
  2. Replies: 1
    Last Post: 10-13-2013, 12:01 AM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  5. characters into array??
    By mik in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2001, 08:54 AM

Tags for this Thread