Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   help wid a programme (http://www.go4expert.com/forums/help-wid-programme-t21474/)

lionaneesh 24Mar2010 00:31

help wid a programme
 
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!!!!!!!!!!!!!

virxen 24Mar2010 04:03

Re: help wid a programme
 
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
#include <string.h>

void count(char array[]);

int main(){
    char line[100];
    printf("Hey user enter some lines : ");
    gets(line);
    count(line);
    getchar();
    return(0);
}
/* FUNCTION count */

void count(char array[]){
    int z=strlen(array);      
    printf("%d\n",z);
}


raju00003 24Mar2010 14:51

Re: help wid a programme
 
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);

  gets(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);
}


xpi0t0s 24Mar2010 14:54

Re: help wid a programme
 
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.


All times are GMT +5.5. The time now is 01:05.