ProgrammingThis 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.
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.
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.
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'
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.
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
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.
/* 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");
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.