Strings Help!

This is a discussion on Strings Help! within the C Programming forums, part of the General Programming Boards category; How would i go about placing a String a user enters, and placing it into a specific location in the ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Strings Help!

    How would i go about placing a String a user enters, and placing it into a specific location in the array name? So when name[2] is called, it will display the string or when name[3] is called it will display the string located in there.

    Code:
    #include <stdio.h>
    
    main()
    {
            int i=0;
            char name[100]={0};
            char userName[ ];
    
            printf("Name - ");
            scanf("%s", &userName);
    
            name[2] = userName;
    
            printf("%s", name[2]);
    }
    Any help would be great.. thx

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Re: Strings Help!

    Originally posted by Cloud@CNET
    How would i go about placing a String a user enters, and placing it into a specific location in the array name? So when name[2] is called, it will display the string or when name[3] is called it will display the string located in there.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int i=0;
            char name[ROWS][100];
            char userName[100];
    
            printf("Name - ");
           fgets(userName, give it a size not unbounded, stdin);
    
            name[2] = userName;  this is wrong 
           you need strcpy(name[2], username);
            /* include <string.h> */ 
           or
    
           fgets(name[index], 100, stdin); 
    
            printf("%s", name[2]);
           
           return EXIT_SUCCESS; 
    }
    Any help would be great.. thx [/B]
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    a string is just a bunch of ints together. one element is just one letter, its probally easier to have 2 seperate strings. some one else on the board could probally show you how to set it up with a matrix if you really want to (it looks easy, i just havnt done it yet).

    unless you mean the user name is the name with some extra in front of it. then you could do something like

    char *spot = username + 2;

    then spot is a string that starts out on the 3 letter.

    and what about char userName[ ], you didnt alocate any space for it. and you dont have to put the & with &userName.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Post

    Thx for the help..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21