Thread: strlen help please

  1. #1
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20

    strlen help please

    I know the my code source is wrong and wont compile but I don't know how I get the value from strlen(fulln) to the int nc;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {   char  fn[31],ln[31],fulln[62];
        int   nc;
    
        printf("What is your first name: ");
        scanf("%s", fn);
        printf("What is your last name: ");
        scanf("%s", ln);
        strcpy(fulln, fn);
        strcat(fulln, " ");
        strcat(fulln, ln);
        strlen(fulln) = nc; // LOL this is what im guessing 
        printf("%d characters contain in you full name", nc);
        fflush(stdin);
        getchar();
      	
      return 0;
    }

  2. #2
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Give that the flop-a-doo. nc = strlen(fulln);

  3. #3
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    Thanks it worked.
    but I don't understand why it wouldn't read the way I wrote it.
    its pretty much the same thing
    2+3=5
    3+2=5

  4. #4
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Those are mathematical expressions. In programming an assignment operator works by setting x to y when x = y. It is not commutative. Which if you think about it makes sense. Wouldn't every line of code be utterly ambiguous if it were?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In math, = means "they are equal" as well as "assign one side with the other", and it's pretty much orthogonal. In C, C++, Pascal, Algol, Fortran, Basic, Modula-2/3, Simula and just about any other language that has assignments, the assignment goes from right to left: So you need a "lvalue" on the left side, which can be assigned to, and a "rvalue" on the right hand side, which gets assigned into the lvalue.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    Yeah now it does. Thanks I love this site you get help so fast.

  7. #7
    Banned
    Join Date
    Dec 2008
    Posts
    49
    You are welcome bro.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. fflush(stdin);
    is undefined - read FAQ

    2. also note that strlen returns size_t so better define nc var of the correct type

    3. Your scanf calls do not have protection from buffer overrun - so better change them to
    scanf("&#37;30s", fn);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM