whts wrong with the following function...

This is a discussion on whts wrong with the following function... within the C Programming forums, part of the General Programming Boards category; Hi, Code: int iStrlen(const char *str1) { int len =0; while(len++,(*str1++ !='

Thread: whts wrong with the following function...

')); return (len); } why does the following function ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    whts wrong with the following function...

    Hi,
    Code:
    int iStrlen(const char *str1)
    {
    	int len =0;
    	while(len++,(*str1++ !='\0'));
    	return (len);
    
    }
    why does the following function returns wrong result:

    length for "google"

    Output:
    6

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Why do you consider the output to be wrong? google is six characters in length.

    ??

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Okay i am sorry....

    It prints 7 for "google"

    It always prints 1 extra....

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You are off by one with your len counter. Try something like this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int iStrlen(const char *str1)
    {
        int len = 0;
        while (*str1++ != '\0') {
            len++;
        }
        return len;
    }
    
    int main(void)
    {
        char txt[] = "google";
    
        printf("txt = '%s'\n", txt);
        printf("strlen(txt) = %zu\n", strlen(txt));
        printf("iStrlen(txt) = %d\n", iStrlen(txt));
    
        return 0;
    }
    You were incrementing len before you were checking for '\0'
    Last edited by kermit; 05-02-2010 at 06:08 PM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    thanks.....Is there an alternative to do the same thing in one line?

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Why do you want it as a one liner? Personally, I am not a real fan of cramming everything into one line (depending on the situation). Anyway, you could do something like the following:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int iStrlen(const char *str1)
    {
        int len;
        for(len = 0; *str1++; len++);
        return len;
    }
    
    int main(void)
    {
        char txt[] = "google";
    
        printf("txt = '%s'\n", txt);
        printf("strlen(txt) = %zu\n", strlen(txt));
        printf("iStrlen(txt) = %d\n", iStrlen(txt));
    
        return 0;
    }
    Using the for loop tends to lend itself more naturally to this task (though some of the more experienced programmers here could undoubtedly demonstrate a nice way to do the same thing with a while loop).
    Last edited by kermit; 05-02-2010 at 06:28 PM.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    thanks....for your time

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Why does the following function throws exception:


    Code:
    void vRevStr(char *ptr ,int len)
    {
    	int a = len/2,i;
    	char *c,*d;
    	d = ptr;
    	c = ptr +len-1;
    	for(i=0;i < a;i++)
    	{
    		*ptr++ ^= *c--;
    		*c-- ^= *ptr++;
    		*ptr++ ^= *c--;
    	}
    	printf("string content:%s",d);
    }
    It throws exception at the highlighted line....what may be the reason?

    Thanks in advace

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not sure, but you can test it with some printf() lines of code being added, and then step through it and see.

    You know that an odd line length won't divide by 2 into equal parts. Does your code handle that correctly? In lines where you are having multiple increments/decrements, post decrements are not always going to work as you'd hope. Be as explicit about the order of operations you want, as possible, and use pre increments/decrements, whenever feasible.

    I'd use the while(low index <= high index) kind of logic for the test. Like in a binary search function.
    Last edited by Adak; 05-02-2010 at 08:34 PM.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    As an aside:

    Question 20.15c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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