Thread: strlen help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    strlen help

    Hi
    I have aslight problem regarding the strlen function. I was told that if you had an array that can hold 20 characters, and you asked the user to input a string, and they entered a string of 7 characters, that the program would print out 'rubbish' due to the string being so small. I was told I could counter this problem by using "strlen".

    This is where I get stuck, i'm not really sure how to implement this into my program.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    
    {
    
       char romanchar[16];
       int counter;
       int sum;
       int lenght;
    
       printf("Please enter a Roman Numeral");
       gets(romanchar);
    
       counter = 0;
       sum = 0;
    
       lenght = strlen(romanchar);     <<<---------------- this is where I get stuck
       
    
       for ( counter = 0; counter < 16; counter++ )
     {
    
       if ( romanchar[counter] == 'I' && romanchar[counter+1] == 'V')
       {
          sum = sum + 4; 
          counter++;
       }
    I know how to use the function if I wanted to print to the screen the total of characters in the string, but I need the function to tell check the lenght of the string and then take that value and use that in the loop with the 'if' statement, so it dosent process any rubbish.

    Any help will be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Your loop would test
    counter < length.

    Also you must read the FAQ on why "void main" and using "gets" are bad ideas.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
     for ( counter = 0; counter < lenght; counter++ )
     {
    
       if ( romanchar[counter] == 'I' && romanchar[counter+1] == 'V')
       {
          sum = sum + 4; 
          counter++;
       }
    That should do the trick.

    And gets is bad news. Read the FAQ about that one and use something else for input.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Thanks so much guys!!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And spell length correctly - otherwise you'll end up confusing yourself and others later on [say you need to use the length somewhere else further down in the code].

    Misspelled names are amongst the worst type of errors you can try to find [only slightly worse would be if you have a global variable called length and a local one called lenght, and you then start wondering what is actually going on, when the one version of the variable holds one value, and you see a completely different value from the other variable in the code twenty lines down!]

    --
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Clarification on C-style strings:
    These strings are stored in an array with a char for each index, and after the string a required NUL char is placed. This char tells strings functions that this is the end of the string. Even if junk is placed after that char, it won't print, because that little magical number in the array will tell the the string ends there.
    strlen is a function that returns the length of a string (not including the NUL char).
    Consider using fgets instead of gets, too, as everyone so happily complains ^_^
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by stewie1986 View Post
    Hi
    I have aslight problem regarding the strlen function. I was told that if you had an array that can hold 20 characters, and you asked the user to input a string, and they entered a string of 7 characters, that the program would print out 'rubbish' due to the string being so small. I was told I could counter this problem by using "strlen".
    Whoever told you that is crazy. If you end up printing "rubbish," that means the string wasn't properly null terminated. But if the string wasn't null terminated, then strlen() cannot possibly find the true length.

    So whoever told you this was, uh, smoking something.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I think what it's possible that the original quote meant that if you print the entire buffer of a string, you'll get bizarre output if the length of the string is less than the size of its buffer. This is just a guess, though, since I think it got confused by the time the OP posted it.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And how are you going to do that? Print them char by char?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Probably. I think the intent was to explain how strings work, and the OP got confused.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So stewie1986, what are your intentions? If you're confused about strings, there are tutorials on the site where you can brush up your skills a little. If you have any questions or are unsure, we can explain.
    Bu I think there's a little confusion as to what you want to know right now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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 in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  3. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  4. 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
  5. strlen
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2002, 11:57 PM