Thread: String length

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    16

    Question String length

    Code:
    #include <string.h>
    #include <stdio.h>
    
    
    void main(int argc, char *argv[])
    {
        char *input = "Enter Any string";
        printf("Your Entered String is: %s\n", input);
    
    
        int length = 0;
        while (input[length] != '\0') {
            length++;     
        }
    
    
        printf("length is : %i\n", length); 
    }
    i need to find the length for each other word separately..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,656
    Perhaps you could also compare
    input[length] != ' '
    as well.

    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.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you could search for the spaces as indicated above then get that every other word then run strlen on it to get its length.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    103
    Take a look at this code:

    Code:
    // Count number of words in a line.
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <ctype.h>
    
    #define LINE_LEN 100
    
    int count(const char * const s);
    bool acceptable_char(int ch);
    bool ignore_ch(int ch);
    
    int main(void)
    {
        char words[LINE_LEN+1]= {'\0'};
        int words_count=0;
    
        do
        {
            printf("Enter a line(just enter to exit):");
    
            char * wo = NULL;
            wo = fgets(words,LINE_LEN-1,stdin);
            if(wo == NULL)
            {
                fprintf(stderr,"fgets failed.\n");
                exit (EXIT_FAILURE);
            }
    
            //printf("words:%s\n",words);
            words_count=count(words);
            printf("Words count is: %d\n\n",words_count);
    
        }
        while(words[0]!='\n');
    
        printf("Finished.\n");
    
        return EXIT_SUCCESS;
    }
    
    int count(const char * const s)
    {
        unsigned idx=1;
        int cnt;
    
        if(ignore_ch(s[0]) )
            cnt=0;
    
        else
        {
            for(cnt=0; s[idx]!='\0'; idx++)
            {
                if(isspace(s[idx]) && acceptable_char(s[idx-1]))
                    cnt++;
            }
        }
    
        return cnt;
    }
    
    bool acceptable_char(int ch)
    {
        bool r;
    
        if(isalpha(ch) || isdigit(ch))// isalnum
            r=true;
        else
            r=false;
    
        return r;
    }
    
    bool ignore_ch(int ch)
    {
        bool r;
    
        if(ch=='\0' || ch=='\n' || ch==EOF)
            r=true;
        else
            r=false;
    
        return r;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  2. string length
    By larrydeloafer in forum C Programming
    Replies: 11
    Last Post: 03-02-2011, 05:23 PM
  3. How to set a string to length of 0
    By youjieus in forum C Programming
    Replies: 1
    Last Post: 10-25-2007, 03:32 AM
  4. std::string length
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 08-26-2005, 07:57 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

Tags for this Thread