Skip to content

Instantly share code, notes, and snippets.

@gotjon05
Last active August 5, 2019 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotjon05/c1955aa66043d7e9ad45dfb6592f39dc to your computer and use it in GitHub Desktop.
Save gotjon05/c1955aa66043d7e9ad45dfb6592f39dc to your computer and use it in GitHub Desktop.
/*
write a program to count blanks, tabs and newlines
getline function
void count_type function
*/
#define MAXLINE 1000
#include <stdio.h>
int getlines(char line[], int maxline);
void count_type(char line[], int array_size);
int main()
{
char line[MAXLINE];
int array_size;
while(array_size = getlines(line, MAXLINE)){
//array_size = sizeof(line)/sizeof(char);
//printf("arraysize: %d", array_size);
count_type(line, array_size);
printf("%s", line);
}
}
//modifys array, adding input
int getlines(char line[], int maxline)
{
int i, c;
for(i=0; i<maxline-1 && (c = getchar()) != EOF && c != '\n'; ++i){
line[i] = c;
}
line[i] = '\0';
return i;
}
// pass array to a function and iterate through it while counting spaces, tabs, and newlines.
void count_type(char line[], int array_size)
{
int i, tab;
tab = 0;
for(i=0; i < array_size; i++){
if(line[i] == '\t'){
++tab;
}
}
printf("%d", tab);
}
@gotjon05
Copy link
Author

gotjon05 commented Aug 5, 2019

The assignment operator in main was evaluating getline to be 0 instead of assining the returning value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment