help wid a programme

Discussion in 'C' started by lionaneesh, Mar 23, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Code:
    #include<stdio.h>
    #define LEGAL 1 
    
    void count(char array[]);
    
    int main()
    {
    
        char line[100];
    
    
        printf("Hey user enter some lines : ");
        scanf("%s",line);
    
        count(line);
        return(0);
    }
    /* FUNCTION count */
    
    void count(char array[])
    {    
        char c;
        int x,z=0;
            /* Wat to do to count \n and \t and blanks */
                for(x = 0 ;(c = array[x]) != '\0' ; x++)
                {        
                    z++;
                }            
        printf("%d\n",z);
    }
    
    HElp me wid this function count..

    wat i m trying to make is tat :-

    this function will count the characters used and tell me in decimal format.

    the main reason for making this function is that it will help in malloc();
    function to allocate exact memory....

    plzzzzzz help.................

    and wen i am putting a space the loop terminates...
    and shows only first charcters eg:-

    if written:-

    aneesh hi

    output:-

    6 characters...

    help!!!!!!!!!!!!!
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    scanf is not suitable for this task
    it stops when it sees a space or enter

    use gets or fgets

    for the count problem there is a command--->strlen

    Code:
    #include<stdio.h>
    #define LEGAL 1 
    [COLOR=Red]#include <string.h>[/COLOR]
    
    void count(char array[]);
    
    int main(){
        char line[100];
        printf("Hey user enter some lines : ");
        [COLOR=Red]gets(line);[/COLOR]
        count(line);
        getchar();
        return(0);
    }
    /* FUNCTION count */
    
    void count(char array[]){ 
        [COLOR=Red]int z=strlen(array); [/COLOR]       
        printf("%d\n",z);
    }
    
     
  3. raju00003

    raju00003 New Member

    Joined:
    Dec 22, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #define LEGAL 1 
    
    void count(char array[]);
    
    int main()
    {
    
       //char line[100];
    
      char* line = new char;
    
        printf("Hey user enter some lines : ");
       //scanf("%s",line);
    
       [B]gets(line);[/B]
    
        count(line);
        return(0);
    }
    /* FUNCTION count */
    
    void count(char array[])
    {    
        char c;
        int x,z=0;
            /* Wat to do to count \n and \t and blanks */
                for(x = 0 ;(c = array[x]) != '\0' ; x++)
                {        
                    z++;
                }            
        printf("%d\n",z);
    }
     
    Last edited by a moderator: Mar 24, 2010
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    fgets is better than gets because it specifies a maximum count and won't overflow the buffer. Use stdin for the file pointer. If the string ends with \n then that is all the user entered; if not, there is more and another call to fgets will pick up the next lot of characters.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice