sakaman
Mar 31 2006, 06:46 AM
hi, some might find this stupid but all i want is an answer,
(language C)
I want to write this loop
for(i = 0; i<lim-1 && (c = getchar())!= '\n' && c != EOF; ++i)
s[i] = c;
without using && or ||
so my answer is:
for(i = 0 ; i<lim -1; ++i)
while ((c = getchar()) != '\n')
while( c!= EOF)
s[i] = c;
is my answer right? is there anyother solution?
groovicus
Mar 31 2006, 10:25 AM
There are many ways to program... does the code that you wrote work?
sakaman
Mar 31 2006, 12:46 PM
well actually it doesn't work
so i am asking
if those two code phrase are equal ?
and if the second is wrong how is it right?
thanks for your help
groovicus
Mar 31 2006, 02:20 PM
Can I se the exact code that you are using? Use the cpde tags so thathe formatting is preserved, and then maybe I will be able to se what is happening.
sakaman
Mar 31 2006, 03:25 PM
well actually it doesn't work
so i am asking
if those two code phrase are equal ?
and if the second is wrong how is it right?
thanks for your help
sakaman
Mar 31 2006, 03:31 PM
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{ int len; /*lenght of the string i just gave*/
int max ; /*the length of the biggest string*/
char line[MAXLINE]; /*save string i just gave */
char longest[MAXLINE]; /*save the biggest string*/
max = 0;
while ((len = getline(line, MAXLINE))> 0)
if (len> max)
{ max = len;
copy(longest, line);
}
if (max>0)
printf("%s", longest);
return 0;
}
/* getline : gets string length*/
int getline(char s[], int lim)
{ int c, i;
for ( i=0; i<lim-1 ; ++i)
while ((c = getchar()) !='\n')
while(c!=EOF)
s[i] = c;
if (c == '\n')
{ s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/*copy : copies to 'from' st 'to' , 'to' is big enough*/
void copy(char to[], char from[])
{ int i;
i =0;
while((to[i] = from[i]) != '\0')
++i;
}
the prgram is suposed to take a numbers of string and return the biggest of all of them
when i give it EOF (with ctrl-d)
sorry form the messy comments but there were in greek
the program didn't work after change the code (with the answer with no && and ||)
actually the the ctrl-d it doesn't work
sakaman
Mar 31 2006, 03:33 PM
i didn't get it how will i preserv the formating
groovicus
Mar 31 2006, 03:39 PM
By using the code tags. When you paste code, you surround it with tags that look like this:[ code][ /code]
Does that code even compile?
sakaman
Mar 31 2006, 04:01 PM
CODE
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{ int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE))> 0)
if (len> max)
{ max = len;
copy(longest, line);
}
if (max>0)
printf("%s", longest);
return 0;
}
int getline(char s[], int lim)
{int c, i;
for ( i=0; i<lim-1; ++i)
while ((c = getchar()) !='\n')
while(c!=EOF)
s[i] = c;
if (c == '\n')
{ s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{ int i;
i =0;
while((to[i] = from[i]) != '\0')
++i;
}
a man of it compiles
sorry i didn't get again
groovicus
Mar 31 2006, 10:25 PM
That code is a mess, but I think for your issue:
CODE
int getline(char s[], int lim){
int c, i;
for ( i=0; i<lim-1; ++i){
if ((c = getchar()) != '\n'){
if(c!=EOF){
s[i] = c;
}
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
}
return i;
}
EDIT: Ignore this completely. I am soo tired at the moment that I am going crosseyed trying to keep the nestings straight.
sakaman
Apr 2 2006, 09:51 AM
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.