Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-23-2002, 12:10 AM
|
#1
|
LQ Newbie
Registered: Dec 2002
Location: San Francisco, CA, USA
Distribution: LFS, Splack, Redhat 6.2
Posts: 5
Rep:
|
getchar()
Hi everybody. I am just beginning to learn the C language.
I am having a problem with a seemingly simple script, but can't figure out why it's not working.
Code:
/* Read ordinary text a character at a time from the program's
* stantard input, and print it with each line reversed from left to right.
* Read until you encounter end-of-data.
*/
#include <stdio.h>
#include <stdlib.h> // May not need all of these
#include <string.h>
#define LIMIT 100
main(){
int string [LIMIT];
int i=0;
int q;
int total;
for (i=0; i<LIMIT; i++){
string[i] = getchar();
if (string[i] == EOF ) {
total = i;
break;
}
}
//total = i;
for ( q=total; q >= 0; q--){
putchar(string[q]);
}
return (0);
}
I've tried a bunch of stuff, and I either get nothing back, or my terminal goes all funky.
Any help would be great. Thanks
|
|
|
12-23-2002, 01:22 AM
|
#2
|
Member
Registered: Aug 2002
Location: Sweden
Distribution: Gentoo
Posts: 43
Rep:
|
change:
total = i;
to:
total = i - 1;
|
|
|
12-26-2002, 02:09 PM
|
#3
|
Member
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398
Rep:
|
One thing I'm missing from that is you don't seem to even test for a new line! I would expect you would do something like...
if (string[i] == '\n') // Aha, a new line.
Be careful though, in the horrible bad world of Micro$oft new lines are a carriage return followed by a linefeed ie. a '\r' followed by '\n'
|
|
|
12-20-2017, 02:46 PM
|
#4
|
LQ Newbie
Registered: Dec 2017
Posts: 21
Rep:
|
another suggestion for change is just before return add a final putchar('\n');
so the prompt on the screen is clear of the reverse line of text your routine created.
Code:
}
//total = i;
for ( q=total; q >= 0; q--){
putchar(string[q]);
}
putchar('\n');
return (0);
}
|
|
|
12-24-2017, 08:54 AM
|
#5
|
Member
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145
Rep:
|
Quote:
Originally Posted by cyent
Be careful though, in the horrible bad world of Micro$oft new lines are a carriage return followed by a linefeed ie. a '\r' followed by '\n'
|
Not so fopen when used on microsoft automatically strips the \r from the input stream putting them back for a write.
That's why the b-binary exists as an fopen option, useless on linux, but on microsoft leaves \r\n pairs intact
|
|
|
12-24-2017, 09:04 AM
|
#6
|
Member
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145
Rep:
|
Change/add
Code:
if(string[i]== EOF) {
string[i]='\0';
total=i-1;
break;
}
Although it doesn't matter in your example it's good practice to nul terminate your strings
|
|
|
12-24-2017, 11:12 AM
|
#7
|
Member
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145
Rep:
|
You don't distinguish between no characters read, EOF being reached and 101 characters enteted. In the first and last case total hasn't been set and will lead to problems.
|
|
|
12-24-2017, 12:20 PM
|
#8
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,628
|
Maybe he fixed the code since December 2002, who knows?
|
|
|
12-24-2017, 02:04 PM
|
#9
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
this gives yu a means to get out of loop
Code:
/* Read ordinary text a character at a time from the program's
* stantard input, and print it with each line reversed from left to right.
* Read until you encounter end-of-data.
*/
#include <stdio.h>
#define LIMIT 100
int main()
{
char buffer [LIMIT], c = '\0';
int i=0;
printf("enter one letter or number\n"
"Then press enter\n"
"Enter # to end before 100\n");
while (c != '#')
{
c = getchar();
buffer[i] = c;
i++;
}
printf("Next\n");
//i value has already been set
for ( ; i > 0; i--)
printf("%c",buffer[i]);
return 0;
}
taking in one at a time, then printing out as if it is a line, is counterintuitive. Unless you read in an entire line then write it (the code) to put it into an array one char at a time, then print out the line backwards, or ...
Code:
char buffer2[100];
printf("enter a string\n");
getchar();
fgets(buffer2,sizeof buffer2, stdin);
b = strlen(buffer2);
for (; b > 0; b--)
printf("%c",buffer2[b]);
printf("\n");
gets a line of text then prints it out backwards.
Last edited by BW-userx; 12-24-2017 at 02:47 PM.
|
|
|
12-24-2017, 02:21 PM
|
#10
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,306
|
Quote:
Originally Posted by keefaz
Maybe he fixed the code since December 2002, who knows?
|
Yes, this was a 15-year dead thread before being awakened, let's allow it to return to its repose.
|
|
1 members found this post helpful.
|
12-24-2017, 02:47 PM
|
#11
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
grrrr, got duped
|
|
|
All times are GMT -5. The time now is 02:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|