Thread: Quick question

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    18

    Question Quick question

    Hello, just wondering if this is possible. What i'm trying to do is to assign the length of a string to an integer variable. This is what i tried, it did not work.

    Code:
        printf("Enter a string\n");
    
        fgets(test_pass, 30, stdin);
    
        strcpy(x,(strlen(test_pass)));
    
        printf("%d", x);
    This was just a test, and it did not compile. Any ideas on how to do this?
    Thanks.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read about what strcpy is supposed to do.
    Edit: Added URL http://www.cplusplus.com/reference/cstring/strcpy/

    Code:
    strcpy(x,(strlen(test_pass)));
    You likely want
    Code:
    x = strlen(test_pass);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    18
    Oh thanks. I tried
    Code:
    strlen(test_pass) = x;
    Didn't work that way. Thanks a lot.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by ItzBlue View Post
    Oh thanks. I tried
    Code:
    strlen(test_pass) = x;
    Didn't work that way. Thanks a lot.
    There is a big difference between

    Code:
    strlen(test_pass) = x; 
    /*              and                          */
    
    x = strlen(test_pass);

  5. #5
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28
    Gotta love that charming signature <3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help, quick question.
    By cmanrules in forum C Programming
    Replies: 6
    Last Post: 12-02-2008, 04:04 PM
  2. A quick question
    By abh!shek in forum C++ Programming
    Replies: 22
    Last Post: 04-20-2008, 11:14 AM
  3. Quick question...
    By Queatrix in forum C++ Programming
    Replies: 12
    Last Post: 05-30-2007, 05:59 AM
  4. Quick Question!
    By dizz in forum C++ Programming
    Replies: 14
    Last Post: 11-21-2002, 07:02 AM
  5. qUICK qUESTION
    By kas2002 in forum C++ Programming
    Replies: 7
    Last Post: 06-02-2002, 04:00 PM