Jump to content

help wid a programme

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
lionaneesh

lionaneesh

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
#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
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
First of all, please do us all a favor and get the latest Opera browser so you can spell check...
I felt like ignoring this post it is that bad... Please make a plan...

Secondly...

When you scanf, c only reads until it finds a space, tab or enter (\n)... and places that in the variable...
To simplify this, if you run your program and type in "hello you" as input, all the variable will get is: "hello"
To change that, change your scanf to:"scanf("%[^\n]",line);"

then...

Your function should look like this:

void count(char array[])

{	

	char c;

	int x,z=0;

	for(x = 0 ; array[x] != '\0' ; x++)//this keeps reading the character array until it hits the end

	{

		if(array[x] == ' ' || array[x] == '\t')//if the character at this position is a space or tab, continue with the loop without adding to 'z'

			continue;

		z++;

	}

	printf("%d\n",z);

}


That will fix your code...

#3
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
read this