Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Sorry I Didn't Have Anyother To Ask


  • Please log in to reply
10 replies to this topic

#1 sakaman

sakaman

  • Members
  • 43 posts
  • OFFLINE
  •  
  • Local time:02:20 AM

Posted 31 March 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?

BC AdBot (Login to Remove)

 


m

#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:20 AM

Posted 31 March 2006 - 10:25 AM

There are many ways to program... does the code that you wrote work?

#3 sakaman

sakaman
  • Topic Starter

  • Members
  • 43 posts
  • OFFLINE
  •  
  • Local time:02:20 AM

Posted 31 March 2006 - 12:46 PM

well actually it doesn't work :thumbsup: :inlove: :flowers:

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 :trumpet:

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:20 AM

Posted 31 March 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.

#5 sakaman

sakaman
  • Topic Starter

  • Members
  • 43 posts
  • OFFLINE
  •  
  • Local time:02:20 AM

Posted 31 March 2006 - 03:25 PM

well actually it doesn't work :thumbsup: :inlove: :flowers:

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 :trumpet:

#6 sakaman

sakaman
  • Topic Starter

  • Members
  • 43 posts
  • OFFLINE
  •  
  • Local time:02:20 AM

Posted 31 March 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

#7 sakaman

sakaman
  • Topic Starter

  • Members
  • 43 posts
  • OFFLINE
  •  
  • Local time:02:20 AM

Posted 31 March 2006 - 03:33 PM

i didn't get it how will i preserv the formating :thumbsup:

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:20 AM

Posted 31 March 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?

#9 sakaman

sakaman
  • Topic Starter

  • Members
  • 43 posts
  • OFFLINE
  •  
  • Local time:02:20 AM

Posted 31 March 2006 - 04:01 PM

#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 :thumbsup:

#10 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:20 AM

Posted 31 March 2006 - 10:25 PM

That code is a mess, but I think for your issue:
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.

#11 sakaman

sakaman
  • Topic Starter

  • Members
  • 43 posts
  • OFFLINE
  •  
  • Local time:02:20 AM

Posted 02 April 2006 - 09:51 AM

thanks man i will try it
:thumbsup: :flowers: :trumpet:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users