#include <stdio.h>

int MyStrlen(const char *si);

int main() {
    char str[] = "a";
    char str1[] = "HELLO";
    char str2[] = "";
    
    printf("Length: %d \n", MyStrlen(str));
    printf("Length: %d \n", MyStrlen(str1));
    printf("Length: %d \n", MyStrlen(str2));
    return 0;
}

int MyStrlen(const char *si) {
    int input;  
    
    for(input = 0; *si; si++)
        input++;   
    
    return input;
}

That was my problem. I was supposed to make a MyStrlen(const char *s1); from scratch, and the closest I could get to it would be what I wrote.

When i put MyStrlen(const char *s1); instead of what is already there, I got errors for:
warning: format '%d' expects type 'int', but argument 2 has type 'size_t'

Any suggestions how I can fix this?

I fixed my code, but I get errors for loss of value from an int to unsigned int....what should I do?

#include <stdio.h>

size_t MyStrlen(const char *s1);

int main(void) 
{
    char str[] = "a";
    char str1[] = "HELLO";
    char str2[] = "";
    
    printf("Length: %d \n", (int)MyStrlen(str));
    printf("Length: %d \n", (int)MyStrlen(str1));
    printf("Length: %d \n", (int)MyStrlen(str2));
    return 0;
}

size_t MyStrlen(const char *s1)
{
    int input;  
    
    for (input = 0; *s1; s1++)
        input++;   
    
    return input;
}

Edited 6 Years Ago by potato4610: n/a

I have some idea's for fixing it, but that's NOT the code that you posted!

I can't fix code that I can't see -- trust me on this.;)

Post the code that you tried, and got this error on, and tell me what compiler you're using, and the answer shouldn't be too difficult.

(Basically, the error is quite right - %d is for int's. size_t will be an unsigned int, so try using %u).

I dont, get it I made most of the fixes. How was this not the code I posted?

I assume I just return (size_t)input; and it will work

You were posting your updated code, at the same time I was writing my post - and looking at your first post, only. I was also looking into my header files on "size_t", which is why my post was so much later than yours (although the post was started before you finished).

Which not only confused you and I, but the forum software, as well (I wound up with a double post).

Did you try %u, and what is your compiler?

To be consistent, I would:

1) remove the cast to int, in the printf() statements
2) change int input to size_t input, in the MyStrLen() function

and

3) use %u for the format identifier in the printf() statements.

That works for me, with no errors or warnings, and gives correct results.

when I use %u, I get:

warning: format '%u' expects type 'unsigned int', but argument 2 has type 'size_t'

#include <stdio.h>

size_t MyStrlen(const char *s1);

int main(void) 
{
    char str[] = "a";
    char str1[] = "HELLO";
    char str2[] = "";
    
    printf("Length: %u \n", MyStrlen(str));
    printf("Length: %u \n", MyStrlen(str1));
    printf("Length: %u \n", MyStrlen(str2));
    return 0;
}

size_t MyStrlen(const char *s1)
{
    size_t input;  
    
    for (input = 0; *s1; s1++)
        input++;   
    
    return input;
}

Somehow, I have to edit my code because MyStrlen may use only one variable other than its formal parameter s1. That variable must be of type const char * and may not be initialized to any expression whose actual numeric value is known (such as 0, 1, etc.). And I have no idea what I need to do.

In your compiler's help files, look up what the correct format identifier is, for size_t.

For me, it's an unsigned int. For you, it could be an unsigned long "%lu", or an unsigned short "%hu", or even have a unique identifier, all it's own.

There are a BUNCH of format identifiers for printf. Also, it has length modifiers, like the "h" in "%hu", that I mentioned just before.

Find that right identifier and length (if length is needed), and you'll be good to go. If you can't find it - Google it, and be sure to mention your compiler's name in your search on Google.

Edit:
P.S. s1 IS a const char *, because the s1 string is only measured - not changed. si is just ONE parameter (even though it has two words to describe it).

Edited 6 Years Ago by Adak: n/a

So the other variable is the counter? Dont I already have the counter? What should I change?

To do what you are doing so far, you don't need to change anything except get the right identifier for your size_t to print out OK.

If you can't find it at all, then yes - cast it to an unsigned int and use %u

But, I need this in my code: MyStrlen may use only one variable other than its formal parameter s1. That variable must be of type const char * and may not be initialized to any expression whose actual numeric value is known (such as 0, 1, etc.).

Logically, you don't need anything else - especially not another const char *.

Can you post the EXACT assignment - something has been misunderstood, I believe.

Write your own version of the strlen library function and name it MyStrlen. Its syntax is
size_t MyStrlen(const char *s1);

and it returns the number of characters in the string in s1, not including the null terminator. MyStrlen may use only one variable other than its formal parameter s1. That variable must be of type const char * and may not be initialized to any expression whose actual numeric value is known (such as 0, 1, etc.). MyStrlen must not call any functions or macros nor do any printing. If you use the sizeof operator in MyStrlen you are doing something wrong. Except when declaring arrays, use only compact pointers, that is, use forms like p++ and *p++ but not p and *(p + i). It may help to write the program using array index notation first, then convert to compact pointers. Test MyStrlen on several literal and variable strings including "a", "HELLO", and "", all of which may be hard coded if you wish.


Thank you so much for taking the time to help me with this. I knew I was close :D

You're welcome, guy.

When it refers to "that variable", it is not describing the "other variable". Teacher's description has jumped back to refer to the si variable.

So you're OK on that point.

What you're not OK on is the use of pointers++. Teacher doesn't want size_t input++ at all.

I'm going to post this, and read just a bit of code.

I believe this is what your teacher wants:

size_t MyStrlen(const char *s1) {
  char *ptr = s1;
  
  while(*ptr != '\0')
    ptr++;
  
  return ptr - s1;
}

Which is the same thing your function was doing, but using only pointers.

I'm used to seeing the return from this, being an int, instead of a size_t, but no matter. If you have trouble getting the size_t to print out right, then just cast it to an int, in the printf() statement.

Edited 6 Years Ago by Adak: n/a

But there is an error:

#include <stdio.h>

size_t MyStrlen(const char *s1);

int main(void) 
{
    char str[] = "a";
    char str1[] = "HELLO";
    char str2[] = "";
    
    printf("Length: %d \n", (int)MyStrlen(str));
    printf("Length: %d \n", (int)MyStrlen(str1));
    printf("Length: %d \n", (int)MyStrlen(str2));
    return 0;
}

size_t MyStrlen(const char *s1) 
{
    char *ptr = s1;
    
    while (*ptr != '\0')
        ptr++;
    
    return ptr - s1;
}

--- Module: main.c (C)
char *ptr = s1;
main.c(30): error 158: (Error -- Assignment to variable 'ptr' (line 30) increases capability)
return ptr - s1;
main.c(35): error 732: (Info -- Loss of sign (return) (int to unsigned int))

Try changing "char" to "unsigned char".

BTW this code is almost letter for letter, right out of "The C Programming Language, by Kernighan and Ritchie, page 103. That's the "Bible" for C programming. Only difference is the return is int in the book.

(and ptr instead of p as the name of the pointer).

And WTF compiler are you using? I want to burn it at the stake!! ;)

Edited 6 Years Ago by Adak: n/a

Fixed it!!! All it needed was: const char *ptr = s1; instead of char *ptr = s1;

Thanks :)

I have to submit it to his compiler and it gives me output such as this.... I thought it worked, but WTF is a lint warning?


THIS IS AN AUTOMATIC MESSAGE - PLEASE DO NOT REPLY OR SEND INQUIRIES
For help, contact the instructor at:
Automated Assignment Checker: v20100710_152948

The results of your exercise submission follow...
E-mail name:
Course:
Assignment #:
Exercise #:
Programmer #:
Time received: 8/16/2010 9:59:48 PM
Message ID: sm_00005DBA_5ebf0e10354d4f3eaa85ae1d4d4a59fc
Indent/Tab increment: 4
Print format: 132-column landscape mode
(You must print in the mode you specify!)
File submitted:
main.c

1 lint warning;

***** NOTE *****
ALL errors/warnings MUST BE CORRECTED before submitting this exercise for final grading unless otherwise approved by the instructor. Failure to do so will result in partial loss of credit.
****************

*** COMPILING/LINKING WITH MICROSOFT COMPILER (http://www.microsoft.com) ***

main.c


*** CHECKING POTENTIAL PROBLEMS WITH GIMPEL PC-lint (http://www.gimpel.com) ***

--- Module: main.c (C)
return ptr - s1;
main.c(35): error 732: (Info -- Loss of sign (return) (int to unsigned int))

--- Global Wrap-up


*** CHECKING STYLE ***

These style checks are not perfect! Some violations might be missed and occasionally bogus warnings might be generated. Please contact me regarding any inconsistencies. You are ultimately responsible for meeting all style requirements for this course. :-)

>> Style checking file: main.c

Edited 6 Years Ago by potato4610: n/a

This question has already been answered. Start a new discussion instead.