B
Buck Rogers
Hi guys!
I am trying to do an excercise from my C book, which coincidently is very
similar to the one in the recent "program" thread.
Task: Write a program that uses redirection to accept input from a disk
file,
counts the number of times the first three letters of the alphabet (a, b,
c, and A, B, C)
occur in the file.
Below is my attempt:
==========================================
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 /*Maximum number of characters in the file */
int main( void)
{
char *ptr;
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;
ptr = malloc(1000 * sizeof(char));
gets(ptr);
for (count = 0; count < MAX; count++) {
if( ptr[count] == 'a' || ptr[count] == 'A')
ctra++;
if( ptr[count] == 'b' || ptr[count] == 'B')
ctrb++;
if( ptr[count] == 'c' || ptr[count] == 'C')
ctrc++;
}
printf("ctra = %d\n", ctra);
printf("ctrb = %d\n", ctrb);
printf("ctrc = %d\n", ctrc);
free(ptr);
return 0;
}
==========================================
Problem: This works fine only if the maximum number of characters in
input.txt
is known(less than 1000).
I've tried including:
=======================
while ((ch = getchar()) != EOF) {
ctr++;
}
========================
The reason for this is so that I can use ctr in a malloc to
allocate enough memory to get all the characters in the file.
ie. ptr = malloc(ctr * sizeof(char));
gets(ptr);
However, because getchar *got* all the characters from stdin, the statement
gets(ptr) gets *nothing* from stdin.
I've been working on this for 2 days. It's driving me NUTTTSSSSS!!!
Can someone please give me a hint or 2?
Thanks in advance guys! Love your work!
Buck.
I am trying to do an excercise from my C book, which coincidently is very
similar to the one in the recent "program" thread.
Task: Write a program that uses redirection to accept input from a disk
file,
counts the number of times the first three letters of the alphabet (a, b,
c, and A, B, C)
occur in the file.
Below is my attempt:
==========================================
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 /*Maximum number of characters in the file */
int main( void)
{
char *ptr;
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;
ptr = malloc(1000 * sizeof(char));
gets(ptr);
for (count = 0; count < MAX; count++) {
if( ptr[count] == 'a' || ptr[count] == 'A')
ctra++;
if( ptr[count] == 'b' || ptr[count] == 'B')
ctrb++;
if( ptr[count] == 'c' || ptr[count] == 'C')
ctrc++;
}
printf("ctra = %d\n", ctra);
printf("ctrb = %d\n", ctrb);
printf("ctrc = %d\n", ctrc);
free(ptr);
return 0;
}
==========================================
Problem: This works fine only if the maximum number of characters in
input.txt
is known(less than 1000).
I've tried including:
=======================
while ((ch = getchar()) != EOF) {
ctr++;
}
========================
The reason for this is so that I can use ctr in a malloc to
allocate enough memory to get all the characters in the file.
ie. ptr = malloc(ctr * sizeof(char));
gets(ptr);
However, because getchar *got* all the characters from stdin, the statement
gets(ptr) gets *nothing* from stdin.
I've been working on this for 2 days. It's driving me NUTTTSSSSS!!!
Can someone please give me a hint or 2?
Thanks in advance guys! Love your work!
Buck.