Thread: How this function can check each element of array?

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    How this function can check each element of array?

    Hi, I am studying about Pointers & String. I hardly understand them. I got many question but I try to ask directly.

    This is a program that counts consonants

    Code:
    int strlen(char *s){
        int n;
        
        for(n=0;*s!='\0';s++)
        if(isalpha(*s))
        n++;
        return n;
        
    }
    
    void main(){
        
        char str[]="I love C";
        
        int len;
        len=strlen(str);
        printf("Lenght of %s is %d", str,len);
        
    }
    I want to know how strlen function works

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well since it's software, you can change it to do whatever you want.

    Say
    Code:
    for(n=0;*s!='\0';s++) {
        if(isalpha(*s)) {
            n++;
            printf("Found char %c, n is now %d\n", *s, n );
        }
    }
    Also, main returns int, not void.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search element in array using function help plz
    By san12345 in forum C Programming
    Replies: 1
    Last Post: 03-01-2016, 10:05 AM
  2. Replies: 8
    Last Post: 02-02-2015, 02:31 PM
  3. Replies: 4
    Last Post: 07-13-2012, 12:46 AM
  4. Replies: 4
    Last Post: 04-13-2012, 09:19 AM
  5. Replies: 3
    Last Post: 03-16-2012, 04:42 AM

Tags for this Thread