Thread: Count the length of strintg without using strlen?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Talking Count the length of strintg without using strlen?

    Is there a way to count the characters in a string without using strlen? I was trying to write my own string length function by using the array....Do
    I have to initialize the char array? Can someone
    just give me a hint please??? Here are part of the
    code I have so far, very very ugly.......I don't
    even know what am I doing anymore......

    #include <stdio.h>
    int length(char[]); /* prototypes the function for the length of the char */
    char oddeven(int); /* prototypes the function for even or odd char length */
    int main(void)


    {
    int x;
    char array[26];
    length(array);
    x=length(array);
    printf("\nThe length of the string is %d characters\n", x);

    return 0;

    }

    int length(char array[]) /* define the function of char length */

    {
    int i = 0;
    /* char array[26];*/ /* 25 characters + 1 for the \0 */
    char c;
    printf("\nPlease enter a string, maximum of 25 characters;\n");

    c = getchar();
    while(c != '\0' && c != '\n')
    {

    array[i++] = c;
    return i;
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Unhappy

    I change the characters function to:

    printf("\nPlease enter a string, maximum of 25 characters;\n");

    gets( array );
    while(array[i] != '\0' && array[i] != '\n')
    {
    i++;
    return i;
    }

    Still didn't work it return 1 for the characters length after I type like 13 letters, also it prints out "Please enter a string, maximum of 25 characters" two time. Why????Please help anyone?

    thank you...

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Angry

    ok I change a few thing again (can you guys see
    I'm trying very hard.. ^__^)

    but, when I run the program, 1)I type in few characters hit return, nothing happen, like it's wating for something, so I type it in again, this time it took the string...2)but it also count the "Enter" key that was push by the user after the characters was enter. Can some one help me sove this two problems?

    thank you......


    int length(char[]); /* prototypes the function for the length of the char */
    char oddeven(int); /* prototypes the function for even or odd char length */
    int main(void)


    {
    int x;
    char array[26];

    printf("\nPlease enter a string, maximum of 25 characters:\n"); <--------
    length(array);
    x=length(array);
    printf("\nThe length of the string is %d characters\n", x);

    return 0;

    }

    int length(char array[]) /* define the function of char length */

    {
    int i = 0; <------
    int c = 0; <------

    while (c != '\n') <-------

    {
    array[i++] = c; <-------
    c = getchar(); <-------

    }
    array[i] = '\0'; <-------
    return i;
    }

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include "stdafx.h"
    
    void Count(char[]);
    
    int main()
    {
    	char array[25];
    	puts("Fill array: ");
    	scanf("%24[^\n]",array);
    	Count(array);
    	return 0;
    }
    
    void Count(char a[])
    {
    	char *ptr = a;
    	int count=0;
    	while(*ptr != '\0')
    	{
    		count++;
    		ptr++;
    	}
    	printf("\nSize is %d",count);
    }
    Something like this will work. I'd use a pointer to traverse the array.
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    Hi Witch_King,

    Thank you very much for helping me...
    but we can't use pointer, because we have not
    cover that in class yet. The teacher want us to
    write the two functions and pass the int value
    back to main.

    void Count(char[]) <---- can't use this too

    she want us to use:

    int length(char []);
    char oddeven(int);

    thank you though.....
    I think I need to get some sleep.....can't come up with anything anymore in my little dumb dumb brain...

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Give me 10 minutes. I'll give you an answer using those functions.
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include "stdafx.h"
    
    int length(char []); 
    void oddeven(int); 
    
    int main()
    {
    	char array[25];
    	int size;
    	printf("Enter string: ");
    	gets(array);
    	size = length(array);
    	printf("size is %d",size);
    	oddeven(size);
    	return 0;
    }
    
    int length(char a[])
    {
    	int count=0;
    	while(true)
    	{
    		if(a[count] == '\0') break;
    		count++;
    	}
    	return count;
    }
    
    void oddeven(int size) 
    {
    	if (size % 2)
    	{
    		printf("\n%d is Odd",size);
    	}
    	else
    		printf("\n%d is even",size);
    }
    I didn't understand why the oddeven returned a character so I changed it but this is the general idea. I would have actually made the array arguement constant because you don't want it changed in the function but it works this way for the purposes of a school assignment.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    10 minutes??? oh my god.....How How???
    I..I...work on this problem for 3 days already...
    >__<...I feel so sorry for myself.....


    Thank you... thank you....

    I'll try to correct my code again.....

  9. #9
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I've sudied C and C++ for 2 years. I have lots of experience. That's the only reason why I can answer this question quickly. You will no doubt still have to make a few adjustments to it though, but you can likely take it from here.
    I compile code with:
    Visual Studio.NET beta2

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Post

    What kind of header is stdafx.h , do we have it on solaris ?


    #include <stdio.h>
    /*return length of a string*/
    int len(char s[])
    {
    int i=0;
    while(s[i]) i++;
    return i;
    }

    /*return 1 if odd and 0 if even */

    int isodd(int i)
    {
    return i&1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. Replies: 7
    Last Post: 01-30-2006, 02:39 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM