C Code formation help

This is a discussion on C Code formation help within the C Programming forums, part of the General Programming Boards category; I need the correct C code for this function string_length, to calculate the number of characters in a string and ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    11

    C Code formation help

    I need the correct C code for this function string_length, to calculate the number of characters in a string and return the answer as an int (without counting the null terminator '\0').

    Code:
    int string_length( char str[ ] )
    {
    
    
    
    
    
    
    
    
    
    }
    Could anyone help me write this out?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Are you really so lazy that you can't even try to write a two-line function? Seriously, that's it, two lines. Here's a hint: it involves a loop. Probably a while loop would be best. Other than that, you have to make some sort of effort if you actually want help. Something more than an empty shell of a function that was probably given to you by your teacher anyhow.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    It's got to be homework. The strlen() function is part of the standard library and does exactly that. Your tutor is trying to demystify this function for you by making you write it yourself.
    It's very easy. Actually it's the very first example in my book, Basic Algorithms.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    11
    Here's what I thought would work:

    Code:
    int string_length( char str[ ] )
    {
         int count = 0;
         
         while ( string[count] != '\0' )
               ++count;
    
         return (count);
    }
    Last edited by Dynesclan; 05-26-2012 at 02:52 PM. Reason: not finished typing

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    So what's the problem?

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    11
    I'm not able to run the code, but I just need to verify that it works in the same way strlen() does.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    So add a main function with a string of known length, and print out the result of your string_length function with that string, to test if it works.

    EDIT: Try "This string has 30 characters." Hopefully I can count. Or you can compare with the result of the standard strlen function Malcom mentioned.
    Last edited by anduril462; 05-26-2012 at 03:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  2. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

Tags for this Thread


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