![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 110
| Printing Length of Input and the Limited Input So I am learning to program in C using the K&R book. I am struggling with a problem, in the book. For those with the book, chapter 1.9, exercise 1-16. For those without the book; The book has given me a piece of code that has the main function and two other functions. The original code simply took in an input of lines, and printed out the longest line as allowed by the MAXLINE number. The exercise now wants me to modify only the MAIN code so that it prints out the actual length of the longest line, and as much as possible of the line as limited by MAXLINE. The way I attempted to do it does not even pass the most simple test, in which I only give it one line. It skips the first character and prints out the rest that is possible according to the limit, but it also returns back a length of 0, which is not valid. Can somebody help me please? This is the original code from the book... Code: #include <stdio.h>
#define MAXLINE 1000
int getLine(char line[], int maxline);
void copy(char to[], char from[]);
//prints longest input line
main(){
int len; //current line length
int max; //maximum length seen so far
char line[MAXLINE]; //current input line
char longest[MAXLINE]; //longest line saved here
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max){
max = len;
copy(longest, line);
}
if (max > 0){ //there was a line
putchar('\n');
printf("%s", longest);
}
return 0;
}
//read a line into s, return length
int getline(char s[], int lim){
int c, i;
for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
//copy from into to: assume to is big enough
void copy(char to[], char from[]){
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
Here is my modified version... Code: #include <stdio.h>
#define MAXLINE 10
int getline(char line[], int maxline);
void copy(char to[], char from[]);
//prints longest input line
main(){
int c; //the char
int len; //current line length
int max; //maximum length seen so far
int maxlen; //longest line
int possiblemax; //possibly max line
char line[MAXLINE]; //current input line
char longest[MAXLINE]; //longest line saved here
//sets default values
max = maxlen = possiblemax = 0;
//while loop for checking end of input
while ((c = getchar()) != EOF){
//if case for when the char is equal to a new line
if (c == '\n'){
//when char is equal to a new line
//and possiblemax is greater than maxlen
//change maxlen to equal to possible max
//reset possible max to 0
if (possiblemax > maxlen){
maxlen = possiblemax;
possiblemax = 0;
}
//else just set possiblemax back to 0
else possiblemax = 0;
}
//if not a new line, add 1 to possiblemax
else ++possiblemax;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max){
max = len;
copy(longest, line);
}
if (max > 0){ //there was a line
//prints maxlen value
printf("length:%d", maxlen);
putchar('\n');
printf("%s", longest);
}
return 0;
}
}
//read a line into s, return length
int getline(char s[], int lim){
int c, i;
for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
//copy from into to: assume to is big enough
void copy(char to[], char from[]){
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
|
| dnguyen1022 is offline | |
| | #2 |
| Why bbebfe is not bbebfe? Join Date: Nov 2008 Location: Earth
Posts: 27
| [I have not run your code] You have two while loops and of which are both get character from stdin. the outer loop eats the first character the user inputted each time. You put the return statement within the outer loop, I think it's the reason your program stops after giving it only one line.
__________________ Do you know why bbebfe is NOT bbebfe? Last edited by bbebfe; 11-19-2008 at 02:26 AM. |
| bbebfe is offline | |
| | #3 |
| Registered User Join Date: Nov 2008
Posts: 110
| Code: #include <stdio.h>
#define MAXLINE 10
int getline(char line[], int maxline);
void copy(char to[], char from[]);
//prints longest input line
main(){
int c; //the char
int len; //current line length
int max; //maximum length seen so far
int maxlen; //longest line
int possiblemax; //possibly max line
char line[MAXLINE]; //current input line
char longest[MAXLINE]; //longest line saved here
//sets default values
max = maxlen = possiblemax = 0;
//while loop for checking end of input
while ((c = getchar()) != EOF){
//if case for when the char is equal to a new line
if (c == '\n'){
//when char is equal to a new line
//and possiblemax is greater than maxlen
//change maxlen to equal to possible max
//reset possible max to 0
if (possiblemax > maxlen){
maxlen = possiblemax;
possiblemax = 0;
}
//else just set possiblemax back to 0
else possiblemax = 0;
}
//if not a new line, add 1 to possiblemax
else ++possiblemax;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max){
max = len;
copy(longest, line);
}
if (max > 0){ //there was a line
//prints maxlen value
printf("length:%d", maxlen);
putchar('\n');
printf("%s", longest);
}
}
return 0;
}
//read a line into s, return length
int getline(char s[], int lim){
int c, i;
for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
//copy from into to: assume to is big enough
void copy(char to[], char from[]){
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
|
| dnguyen1022 is offline | |
| | #4 |
| Registered User Join Date: Sep 2008
Posts: 46
| In your getline function, your for loop is missing brackets after it. Code: int getline(char s[], int lim){
int c, i;
for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i){
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
}
return i;
}
|
| blurx is offline | |
| | #5 |
| Registered User Join Date: Nov 2008
Posts: 110
| That is exactly the way the K&R book gave it to me...possible typo? Or maybe the authors have their own reason for doing that? |
| dnguyen1022 is offline | |
| | #6 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,680
| > In your getline function, your for loop is missing brackets after it. No it isn't. The single-line for loop does all the work. > However, what can I do to prevent my first letter from being "eaten"? Why do you even need the first while loop? The while ((len = getline(line, MAXLINE)) > 0) does the right thing with EOF anyway. Your attempt to detect an early EOF is just messing things up. |
| Salem is offline | |
| | #7 |
| Registered User Join Date: Nov 2008
Posts: 110
| Hmm..I understand what you mean...but now how would I go about finding the length of the line without being limited by the number passed into the getline function? |
| dnguyen1022 is offline | |
| | #8 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,680
| In the code which calls getline(), detect whether the returned line contains a \n (or not) to decide whether the line is really long. Unless you want to make getline a lot more complicated with calls to malloc and realloc, to expand the space as the user keeps typing. |
| Salem is offline | |
| | #9 |
| Registered User Join Date: Nov 2008
Posts: 110
| I think I understand what you are saying. However is it possible to change the array so that its able to fit new characters? Would I have to make a new array for the purpose of "overmax" lines or can I change the array size? Also would I add something that is similar to Code: if (line[MAXLINE] != '\n'){
?expand the array?
}
|
| dnguyen1022 is offline | |
| | #10 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #11 |
| CSharpener Join Date: Oct 2006
Posts: 5,324
| line[MAXLINE] this is out of bounds access, the valid indexes are from 0 to MAXLINE - 1
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #12 |
| Registered User Join Date: Nov 2008
Posts: 110
| Hmm...alright I'll change it around and see what I get, with the MAXLINE - 1 in mind. Also silverlight, I apologize if this sounds rude, but I am trying to follow the book and using the information that they have given me so far to solve this problem. I believe I should solve this problem with the basics before getting into more complicated stuff or by using shortcuts. I appreciate your help and still want your help to solve this problem but in a less advanced way...for now. |
| dnguyen1022 is offline | |
| | #13 |
| Registered User Join Date: Nov 2008
Posts: 110
| So..I tried it again...this time my code does not seem to be doing anything. I think I have the concept down. However my code is wrong....I think the Code: (c = getchar()) Code: #include <stdio.h>
#define MAXLINE 10
int getLine(char line[], int maxline);
void copy(char to[], char from[]);
//prints longest input line
main(){
int len; //current line length
int max; //maximum length seen so far
int possiblemax; //possible maximum length
int realmax; //the real maximum length
int c; //the char
char line[MAXLINE]; //current input line
char longest[MAXLINE]; //longest line saved here
max = possiblemax = realmax = 0;
while ((len = getline(line, MAXLINE)) > 0)
//checks to see if array ends with a new line
if (line[len - 1] != '\n'){
//while c is not end of file
while ((c = getchar()) != EOF){
//if c is not equal to a new line
if (c != '\n'){
//while c is not a new line add one to possible max
while(c != '\n'){
++possiblemax;
}
}
//else if is equal to a new line
else if (c == '\n'){
//if possiblemax is greater than real max
//set realmax to possiblemax
//reset possible max
if (possiblemax > realmax){
realmax = possiblemax;
possiblemax = 0;
}
//otherwise reset possiblemax
else possiblemax = 0;
}
}
}
if (len > max){
max = len;
copy(longest, line);
}
if (max > 0){ //there was a line
printf("length:%d", realmax);
putchar('\n');
printf("%s", longest);
}
return 0;
}
//read a line into s, return length
int getline(char s[], int lim){
int c, i;
for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
//copy from into to: assume to is big enough
void copy(char to[], char from[]){
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
|
| dnguyen1022 is offline | |
| | #14 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,825
| Code: while(c != '\n'){
++possiblemax;
}
|
| tabstop is online now | |
| | #15 |
| Registered User Join Date: Nov 2008
Posts: 110
| Wouldn't it stop when it gets to a new line or when it reaches EOF due to the while loop that it is in? Also it is suppose to get the length of the REST of the line..since the getline stops whenever it reaches its MAXLINE limit. The main purpose of the program is to print out the length of the longest line, and print out as much as possible of the line as limited by MAXLINE. |
| dnguyen1022 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|