Thread: Writing a code without arrays!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Writing a code without arrays!

    Hello, I am new to the C language. I am in need of help and/or advice on my code. I have to create a program that will transform a user's first and last name into their last name and their first name but with an initial. I am having trouble with writing the code and I will appreciate with advice. If this seems like I'm intending for someone to 'do my homework' then I apologize for the misunderstanding as that is not my intention.
    Code:
    
    #include <stdio.h>
    int main();
    {
        char ch;
        printf("Please enter your first and last name:");
        scanf("%d", &ch);
        printf("%d", &ch);
        getchar();
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you are trying to do strings without using arrays all I can say is...


    Well, good luck with that.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Darn.... thanks!

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    A String is just an array of characters. You can either use an array like this
    Code:
    char ch[32];
    or you can allocate memory to a pointer and treat it as an array.

    So like Tater implied, what you want doesn't really make sense.

    Anyways, your scanf and printf lines are already incorrect.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    That depends what your definition of "array" is.

    If it's just a variable that has brackets, your challenge is easy, just use malloc(), and it's sometimes not considered an array, just a space in memory that data is stored sequentially.

    Otherwise, you might have to either do it character by character, or maybe mmap() using void..

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by failure67 View Post
    A String is just an array of characters. You can either use an array like this
    Code:
    char ch[32];
    or you can allocate memory to a pointer and treat it as an array.

    So like Tater implied, what you want doesn't really make sense.

    Anyways, your scanf and printf lines are already incorrect.
    I will put this to use and thank you for the pointers.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    I shall provide some more information tomorrow. Thank you, memcpy and Tater for the help.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by memcpy View Post
    That depends what your definition of "array" is.
    You're kidding ... right?

    About the only definition of "array" that even makes sense is "a sequential group of memory addresses used to store information".

    No amount of semantic self-diddling is going to get around the simple fact that C stores strings in arrays...
    Last edited by CommonTater; 01-10-2012 at 10:50 PM.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by CommonTater View Post
    You're kidding ... right?

    About the only definition of "array" that even makes sense is "a sequential group of memory addresses used to store information".

    No amount of semantic self-diddling is going to get around the simple fact that C stores strings in arrays...
    This is technically true, but I also said "If it's just a variable that has brackets", meaning the "char d[3];" notation. I then said the "depending on the definition", because I've seen people (incorrectly) only consider bracket notation arrays, and malloc'd areas something else. I was trying to clarify whether the OP wanted to just avoid brackets and worded it wrong, or wanted to change the actual array part.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    This is what I have so far. I'm not confident with how far I have gotten though :\
    Code:
    #include <stdio.h>
    char main()
    {
    char ch1, ch2;
    printf("Please enter your first and last name:");
    scanf("%d", &ch1, &ch2);
    printf("This is your name: %d", ch2, ch1);
     
    getchar();
    return 0;
    }

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to use an array of chars to hold a person's name. And you need to use %s as the scanf format specifier.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    char main()
    Incorrect, it should be
    Code:
    int main()
    Maybe you should copy and paste your assignment. You may be misunderstanding it.

  13. #13
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by rags_to_riches View Post
    Code:
    char main()
    Incorrect, it should be
    Code:
    int main()
    Maybe you should copy and paste your assignment. You may be misunderstanding it.
    Thanks for the correction. I had some help and this is the end result.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define BLANK ' '
    
    
    
    
    int main()
    {
        char init,ch;
    printf("Enter First Name followed by Last Name:");
        init=getchar();    /* Init is initial. */
        
    while((ch=getchar())!=BLANK) /* Initials the first name */
            ;
    while((ch=getchar())==' ') /* Creates the space between the name */
            ;
        printf("%c",ch);
        while((ch=getchar())!='\n')
            printf("%c",ch);
        
    printf("%c.\n", init); /* Creates the initial with the first name */
        
    }
    Last edited by !XOBILE; 01-11-2012 at 03:24 PM.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Except that it doesn't leave a space between the last name and initial. You might need to fix that.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Defining BLANK is worse than pointless. Just use ' '.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  2. Help with writing functions using arrays
    By chrisjg04 in forum C Programming
    Replies: 6
    Last Post: 03-18-2010, 07:59 PM
  3. Reading and writing top arrays
    By qwertysingh in forum C Programming
    Replies: 5
    Last Post: 02-05-2009, 09:56 PM
  4. writing arrays of structures
    By chrismiceli in forum C Programming
    Replies: 6
    Last Post: 06-26-2003, 06:50 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM