The program is suppose to print out the longest line in all the outputs.But the program is acting pretty weird. Sometimes it doesn't terminate even when the EOF(ctrl+Z) is triggered and some times it prints blank or weird symbols. I don't why it is not working; can somebody help me fix it please?
//START
#include <stdio.h>
#include <stdlib.h>
#define mx 100
int main(void)
{
int line[mx],lng[mx],c,word,maxim;
word=1;
maxim=10;
int i=0;
while((c=getchar())!=EOF)
{
while((c=getchar())!='\n')
{
line[i]=(c=getchar());
if(((c=getchar())==' ') || ((c=getchar())=='\t'))
{
word++;
}
i++;
}
if(woasdrd>=maxim)
{
for(int d=0;d<=99;d++)
{
copyline(lng[d],line[d]);
}
word=1;
i=0;
}
else
{
i=0;
word=1;
}
}
for(int g;g<=99;g++)
{
putchar(lng[g]);
}
}
copyline(int to[],int from[])
{
for(int i=0;i<=99;i++)
{
to[i]=from[i];
}
}
//END
#include <stdio.h>
#include <stdlib.h>
#define mx 100
//copy string array from from[] to to[],and both of it is end with '\0'
void copyline(char to[],char from[])
{
int i = 0;
while( from[i] )
{
to[i]=from[i];
i++;
}
}
int main(void)
{
char line[mx] = { ' ' },lng[mx] = { ' ' }; //line keep the line you just input,lng keep the longest line
int maxim , c; //maxim keep the longest num of longest line
int i=0;
maxim=0;
//get input from stdin ,if EOF then end(Ctrl + C or Ctrl + d is EOF)
while( ( c = getchar() ) != EOF )
{
//if you input a Enter then compare it's length with maxim
if( c == '\n' )
{
line[i++] = '\0'; //turn '\n' into '\0',for string end with '\0'
if( i > maxim ) //compare the line you just input with the longest line,if get longer one,copy it to lng
{
maxim = i;
copyline( lng , line );
lng[ i ] = '\0'; //for string end with '\0'
}
i = 0; //if you get a '\n' ,then you should be ready for next input line,so i = 0,and continue for new get
continue;
}
line[i++] = c; //keep input to line
}
//that's output,for string end with '\0',so put it as condition for while loop
i = 0;
while( lng[i] )
{
printf("%c",lng[i++]);
}
printf("\n");
}
maybe this is what you want,at first I want to improve your code,but it have much errors,include logic and code,so I rewrite your code.if you have problem with this code,please let me know.
I think you are calling getchar() in too many places. You are removing a character from input every time you call that. Really you should call c=getchar() once before the first while loop to read the first character. Remove the rest of the c=getchar() and just use c. Then at the at the end of the inner while loop right after the i++; you should use c=getchar() to read the next character.
Related
So I'm trying to do a program that reads a sequence of numbers separated by spaces and new lines. The output should be the same sequence, but erasing unnecessary zeros(The sequence of charachters 'EOF' ends the program). Per example
01492 102934 should come out as 1492 102934
9312 0 01923 should come out as 9312 0 1923
0001249 0000 should come out as 1249 0
Well I've achieved that purpose but have come across a roadblock. The program doesn't exit unless I type the EOF sequence. Maybe it's because I have a while(1) running that gives an infinite loop. But when I try to delete it the program doesn't even print at all. I'm still learning this is for a school project.
Any help would be apreciated!
Here's the code:
#include <stdio.h>
int main(){
char c;
int i=0;
while(1){
c=getchar();
if (i==0){
if(c=='0'){
while (c=='0'){
c=getchar();
}
}
printf("%c",c);
i=i+1;
}
else if (c==' '){
printf("%c",c);
c=getchar();
if(c=='0'){
while (c=='0'){
c=getchar();
}
}
printf("%c",c);
}
else if (c=='E'){
c=getchar();
if (c=='O'){
c=getchar();
if(c=='F'){
printf("\n");
return 0;
}
}
}
else{
printf("%c",c);
}
}
}
The important stuff:
int c; // IMPORTANT, cannot be char
while (1) {
c = getchar();
if (c == EOF) break; // exit loop
// ...
}
There has to be some way to tell the program to exit.
With this, the program will exit on the letter x or two consecutive newlines or entering END.
getchar will return EOF when there is nothing left to read from a file. That can be simulated from stdin ( the keyboard) with ctrl + z on Windows or ctrl + d on Linux.
#include <stdio.h>
#include <string.h>
int main ( void) {
char done[4] = "";
int c = 0;
int prior = 0;
int reading = 0;
int zero = 1;
while ( EOF != ( c = getchar ( )) && 'x' != c) {
if ( '\n' == c && '\n' == prior) {
break;
}
if ( c >= '0' && c <= '9') {
reading = 1;
if ( '0' != c) {
zero = 0;
}
if ( ! zero) {
putchar ( c);
}
}
else {
if ( reading) {
if ( zero) {
putchar ( '0');
}
if ( ' ' == c || '\n' == c) {
putchar ( c);
}
else {
putchar ( ' ');
}
}
reading = 0;
zero = 1;
}
prior = c;
done[0] = done[1];
done[1] = done[2];
done[2] = c;
done[3] = 0;
if ( 0 == strcmp ( done, "END")) {
break;
}
}
putchar ( '\n');
return 0;
}
getchar() returns an int, not a char. If it only returned a char, there would be no way for it to return a value that indicates end of file, since all char values are valid and can’t be used for another purpose.
A motivating example in decimal system may be: A function checks the temperature returns a two-digit number. Any temperature between 0 and 99 is valid. How do you report errors when the thermometer is disconnected? You have to return a number with more digits, and use a special value like UNPLUGGED = 100.
But int is a wider type: it has many more values than char, and the “extra” values can be used to indicate some special condition that means “hey, this is not a valid character, but something else I had to tell you”.
getchar() returns the EOF constant upon failure (any failure), for example if no more input is available. There’s nothing sensible you can do even if the reason for the failure other than end of input. You should end processing at the first EOF.
Thus, change the type of c to int, and every time you call getchar(), you must check that its value is not EOF, and return when you encounter it.
The nested structure of your loops means that EOF checking has to be repeated all over the place. There are other ways to structure the code to keep this check in one place, but, admittedly, the nested loops have at least the potential to exploit the branch predictor, whereas a single getchar followed by a state-machine style switch statement will make it perform potentially worse. None of this matters in a simple homework problem, but it’s something to keep in mind. In any case, performance has to be benchmarked - no other way around it.
Try this code, I think it does what you requested:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int getLine(char *prmpt, char *buff, size_t sz) {
int ch, extra;
// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf("%s", prmpt);
fflush(stdout);
}
if (fgets(buff, sz, stdin) == NULL)
return -2;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff) - 1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? -1 : 0;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff) - 1] = '\0';
return 0;
}
int* convert2numbers(char* arr, int size) {
int i;
int j;
int k;
char token[100];
int* numbers;
int last_space = 0;
int index = 1;
int amount = 1;
// Count the amount of tokens.
for (i = 0; i < size; ++i) {
if (arr[i] == ' ') {
++amount;
}
}
numbers = (int *)malloc(amount * sizeof(int));
numbers[0] = amount;
for (j = 0; j <= size; ++j) {
if (arr[j] == ' ' || arr[j] == '\0') {
// Copy token from input string.
for (k = 0; k < j; ++k) {
token[k] = arr[k + last_space];
}
token[j] = '\0';
numbers[index] = atoi(token);
// Clear the token and continue.
memset(token, '\0', sizeof(token));
last_space = j;
++index;
}
}
return numbers;
}
int main(void) {
int i;
int size;
int* numbers;
int amount;
char input[100];
char help[] = "Numbers> ";
printf("Input numbers below or press enter to exit!\n");
while (1) {
getLine(help, input, sizeof(input));
// If input is empty exit.
if (input[0] == '\0') {
break;
}
size = strlen(input);
numbers = convert2numbers(input, size);
amount = numbers[0];
for (i = 1; i < amount + 1; ++i) {
printf("%d ", numbers[i]);
}
printf("\n");
}
return 0;
}
When run with these inputs this code outputs:
Input numbers below or press enter to exit!
Numbers> 01492 102934
1492 102934
Numbers> 9312 0 01923
9312 0 1923
Numbers> 0001249 0000
1249 0
Also if you press enter in console, it exits, as to escape the while(1) loop, easily.
I wrote a program to find the longest word in a string and print the number of letters in the longest word. But the code is not printing. I analyzed the program many times but I could not find the solution.
#include <stdio.h>
#include <string.h>
int main() {
char string[100] = "Hello Kurnool";
int i = 0, letters = 0, longest = 0;
start:
for (; string[i] !=' '; i++) {
letters++;
}
if (letters >= longest)
longest = letters;
if (string[i] == ' ') {
letters = 0;
i++;
goto start;
}
printf("%d", longest);
return 0;
}
Using goto is highly discouraged. You should convert your code to use a loop.
The main problem in your code is you do not stop the scan when you reach the end of the string.
Here is a modified version:
#include <stdio.h>
int main() {
char string[100] = "Hello Kurnool";
int i, letters, longest = 0, longest_pos = 0;
for (i = 0; string[i] != '\0'; i++) {
for (letters = 0; string[i] != '\0' && string[i] != ' '; i++) {
letters++;
}
if (letters > longest) {
longest = letters;
longest_pos = i - longest;
}
}
printf("longest word: %d letters, '%.*s'\n",
longest, longest, string + longest_pos);
return 0;
}
Note that the implementation can be simplified into a single loop:
#include <stdio.h>
int main() {
char string[100] = "Hello Kurnool";
int i, start = 0, longest = 0, longest_pos = 0;
for (i = 0; string[i] != '\0'; i++) {
if (string[i] == ' ') {
start = i + 1;
} else {
if (i - start > longest) {
longest = i - start;
longest_pos = start;
}
}
}
printf("longest word: %d letters, '%.*s'\n",
longest, longest, string + longest_pos);
return 0;
}
Below is my approach. You should use C's string manipulation functions. This is the correct way to deal with strings in C.
In the code below, first I acquire the required bytes to store the input string in heap. Then I use strtok to split the string into tokens based on a delemeter and get the length of each sub string. Finally I free the space that I have allocated with malloc.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define phrase "Hello Kurnool"
int main()
{
char* string = malloc(strlen(phrase)+1);
strcpy(string,phrase);
int longest=0;
char *token;
char delimeter[2] = " ";
/* get the first token */
token = strtok(string, delimeter);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );
if(longest < strlen(token)){
longest = strlen(token);
}
token = strtok(NULL, delimeter);
}
printf("%d",longest);
free(string);
return 0;
}
People say - dont use goto but there is nothing inherently wrong with goto. Only thing is if goto is not used judiciously, it makes code more difficult to understand and maintain. For example, the way you have used it in your program ( instead of goto, a loop is perfect fit in such cases). Check this:
To use goto or not?
What is wrong with using goto?
Coming to your code, the for loop condition does not have check for terminating null character
for (; string[i] !=' '; i++) {
Hence it will not stop at the end of string.
To find the number of letters in longest word of string, you can do:
#include <stdio.h>
#include <string.h>
int main() {
char string[100] = "Hello Kurnool";
int i, letters = 0, longest = 0;
for (i = 0; string[i] != '\0'; i++) {
if (string[i] != ' ') {
letters++;
if (letters > longest) {
longest = letters;
}
} else {
letters = 0;
}
}
printf("longest : %d\n", longest);
return 0;
}
First of all,Please avoid using Goto, it is not a good practice.
Secondly, your loop will run infinite times when it iterates the second time because:
for(;string[i]!=' ';i++) // Here String[i] will never be equal to ' ' As there is no white space after your last word.
You can never expect what might be going wrong with your program if you are using
goto statement
which is never advisable to use rather it's bad programming if you use it. Secondly it looks like you are stuck in an infinite loop so her is a solution to your problem:
#include<stdio.h>
#include<string.h>
void main()
{
char s[1000];
scanf("%s",s);
int i=0;
int letters;
int longest=0;
while(s[i]!=NULL)
{
if(s[i]==' ')
{
if(longest>=letters)
{longest=letters;}
letters=0;
}
else
{letters++;}
}
printf("%d\n",longest);
}
So, what I have done is assuming a string s which is the input given by the user. You itterate through s till the last input given by the user after which it encounters a NULL character. Now you are searching for the length of the longest word, so you create a variable letters for counting the no. of letters in each word of the string. And if the string s encounters a space indicating the end of a word, then you check if the variable longest is greater than or less than the word count. And again you initialize letters to 0, so that it can start counting the next word from 0 again.So, by this method at the end i.e. after the while loop terminates we get our required output which is stored in the variable longest.
So, I guess this will print the no. of letters in the longest word.
I'm trying to create a C program that accepts a line of characters from the console, stores them in an array, reverses the order in the array, and displays the reversed string. I'm not allowed to use any library functions other than getchar() and printf(). My attempt is below. When I run the program and enter some text and press Enter, nothing happens. Can someone point out the fault?
#include <stdio.h>
#define MAX_SIZE 100
main()
{
char c; // the current character
char my_strg[MAX_SIZE]; // character array
int i; // the current index of the character array
// Initialize my_strg to null zeros
for (i = 0; i < MAX_SIZE; i++)
{
my_strg[i] = '\0';
}
/* Place the characters of the input line into the array */
i = 0;
printf("\nEnter some text followed by Enter: ");
while ( ((c = getchar()) != '\n') && (i < MAX_SIZE) )
{
my_strg[i] = c;
i++;
}
/* Detect the end of the string */
int end_of_string = 0;
i = 0;
while (my_strg[i] != '\0')
{
end_of_string++;
}
/* Reverse the string */
int temp;
int start = 0;
int end = (end_of_string - 1);
while (start < end)
{
temp = my_strg[start];
my_strg[start] = my_strg[end];
my_strg[end] = temp;
start++;
end--;
}
printf("%s\n", my_strg);
}
It seems like in this while loop:
while (my_strg[i] != '\0')
{
end_of_string++;
}
you should increment i, otherwise if my_strg[0] is not equal to '\0', that's an infinite loop.
I'd suggest putting a breakpoint and look what your code is doing.
I think you should look at your second while loop and ask yourself where my_string[i] is being incremented because to me it looks like it is always at zero...
I'm trying to create a program that checks if a given array/string is a palindrome or not and its not working. The program just prints "0" on every given array, even on palindromes.
int main()
{
char string[100]= {0};
char stringReverse[100]= {0};
int temp = 0;
int firstLetter = 0;
int lastLetter = 0;
printf("Please enter a word or a sentence: ");
fgets(string, 100, stdin);
strcpy(stringReverse , string); // This function copies the scanned array to a new array called "stringReverse"
firstLetter = 0;
lastLetter = strlen(string) - 1; //because in array, the last cell is NULL
// This while reverses the array and insert it to a new array called "stringReverse"
while(firstLetter < lastLetter)
{
temp = stringReverse[firstLetter];
stringReverse[firstLetter] = stringReverse[lastLetter];
stringReverse[lastLetter] = temp;
firstLetter++;
lastLetter--;
}
printf("%s %s", stringReverse, string);
if ( strcmp(stringReverse , string) == 0)
{
printf("1");
}
else
{
printf("0");
}
}
Lets say we implement a simple fun to do that
int check_palindrome (const char *s) {
int i,j;
for (i=0,j=strlen(s)-1 ; i<j ; ++i, --j) {
if (s[i] != s[j]) return 0; // Not palindrome
}
return 1; //Palindrome
}
I think this is far more simpler ;)
For the code posted in question:
Be aware of fgets(). It stops in the first '\n' or EOF and keeps the '\n' character.
So if you give radar for ex, the result string will be "radar\n", which doesn't match with "\nradar"
The Problem:
Let's say you enter the string RACECAR as input for your program and press enter, this puts a newline character or a '\n' in your buffer stream and this is also read as part of your string by fgets, and so your program effectively ends up checking if RACECAR\n is a palindrome, which it is not.
The Solution:
After you initialize lastLetter to strlen(string) - 1 check if the last character in your string (or the character at the lastLetter index is the newline character (\n) and if so, decrease lastLetter by one so that your program checks if the rest of your string (RACECAR) is a palindrome.
lastLetter = strlen(string) - 1; //because in array, the last cell is NULL
// Add these 2 lines to your code
// Checks if the last character of the string read by fgets is newline
if (string[lastLetter] == '\n')
lastLetter--;
fgets adds a '\n' at the end.
So if the user entered "aba", string contains "aba\n".
reverseString contains "\naba".
So it doesn't match.
After the fgets, add this code
int l = strlen(string) - 1;
string[l] = 0;
This will strip out the '\n' at the end before copying it to reverseString.
That aside, you can do this whole program inplace without the need of a second buffer or strcpy or strlen calls.
You have several issues in your code:
first you forgot the last closing brace };
then you forgot to remove the trailing \n (or maybe also \r under Windows) in string;
you don't need to revert the string into a new string; a one-pass check is enough:
Here is a working code:
#include <stdio.h>
#include <string.h>
int main()
{
char string[100]= {0};
int temp = 0;
int firstLetter = 0;
int lastLetter = 0;
printf("Please enter a word or a sentence: ");
fgets(string, 100, stdin);
firstLetter = 0;
lastLetter = strlen(string) - 1; //because in array, the last cell is NULL
while ((string[lastLetter]=='\n')||(string[lastLetter]=='\r')) {
lastLetter--;
}
// This while reverses the array and insert it to a new array called "stringReverse"
temp = 1;
while(firstLetter < lastLetter)
{
if (string[firstLetter] != string[lastLetter]) {
temp = 0;
break;
}
firstLetter++;
lastLetter--;
}
if ( temp )
{
printf("1");
}
else
{
printf("0");
}
}
You can do it by this simpleway also.
#include <stdio.h>
#include <string.h>
int main()
{
char string[10], revString[10];
printf("Enter string for reversing it...\n");
scanf("%s", string);
int stringLength = strlen(string);
for(int i = 0; string[i] != '\0'; i++, stringLength--)
{
revString[i] = string[stringLength - 1];
}
if(strcmp(string, revString) == 0)
printf("Given string is pelindrom\n");
else
printf("Given string is not pelindrom\n");
}
#include<stdio.h>
#include<string.h>`enter code here`
void fun(char *a);
int main ()
{
char p[100];
char *s=p;
printf("enter the string");
scanf("%[^\n]",s);
fun(s);
}
void fun(char *a)
{
if(*a && *a!='\n')
{
fun(a+1);
putchar(*a);
}
}
// use this approach better time complexity and easier work hope this helps
This is a very peculiar doubt about an exercise form the K&R textbook, I don't have any idea what the "standard" answer is, so the program itself may be a bit unfamiliar.
I tried to describe in the code the incremental steps that brought this program together, my question is about an adjustment that by logic shouldn't have any impact but taking away a stray --i.
Instead, if I try it all kind of strange behaviours appear (I tried a few combinations so I won't go to the extent of desribing them all here.. )
#include <stdio.h>
#define MAXCHAR 15
int storeline(char line[], int lim);
void reverse(char in[], char out[], int len);
main() {
int l;
char line[MAXCHAR+2]; /*I add two position to accomodate a newline and a '\0' character */
char enil[MAXCHAR+2];
while ((l=storeline(line, MAXCHAR+2))>=0) {
if (l<MAXCHAR)
reverse(line, enil, l); /*reverse is called with the array (in this case a 17 characters array) and the count computed in storeline (up to )*/
else
reverse(line, enil, MAXCHAR);
printf("%s", enil);
}
return 0;
}
int storeline(char s[], int lim) {
int i;
int c;
for(i=0;i<lim-2&&(c=getchar())!='\n'&&c!=EOF;++i){ /* this loop breaks at i==15 */
if (c==' '||c=='\t') {
while((c=getchar())==' '||c=='\t');
s[i]=' ';
++i;
if(c==EOF)
break;
}
s[i]=c;
}
if (c!=EOF) {
s[i]='\n'; /* a newline is added in s[15] */
++i;
}
s[i]='\0'; /* a '\0' character is added at s[16] */
--i; /*no more characters have to be added so I bring the count of the characters down by 1 (a further unit is deducted by the fact that one character is stored in s[0] */
while (c!='\n' && c!= EOF) {
c=getchar();
i++;
}
return i; /* the count goes on and is subsequently returned by the function, newline is assumed to be a file break by design, but this is easily adjusted */
}
/* let's pretend the string was '123451234512345' and MAXCHAR is 15. */
void reverse (char in[], char out[], int len) {
int i, lim;
i=0;
lim=len-1; /*len was 15, now it is 14. note that the array goes up to in[16] */
while(lim>=0) {
out[i]=in[lim];
++i;
--lim;
}
out[i]='\n';
++i;
out[i]='\0';
}
My doubt is, if I remove the --i element in storeline and decrease lim to len-2 everything should work as before, but it doesn't/ Why? The numbers are literally the same..
I am not sure if I am getting it, but you are trying to mix up 1.18 and 1.19. By one side, you want to clean the input and, by the other, you are trying to reverse the line. I would suggest you divide what you want to do into different functions. It will not only make it easier to program but also will make it easier to detect errors. This way, I would make one function to get the line and store it into an array, exactly equal to the example given in the book. Then, I would do a second function to clean the lines and a third function which reversed the lines. However, if you want only to solve exercise 1.19, you only need to use the getline and reverse functions. I leave to you the part of writing the main() function.
This way, the getline function:
int getline(char line[], int maxsize)
{
int position, input_character;
input_character = 0;
for (position = 0; position < maxsize - 2 && (input_character = getchar()) != EOF && input_character != '\n'; ++position) {
line[position] = input_character;
}
if (input_character == '\n') {
line[position] = input_character;
++position;
}
line[position] = '\0';
return position;
}
The clean function:
void clean(char output[], char input[])
{
char storage[MAXLINE];
int output_character, storage_character, input_character;
output_character = 0;
storage_character = 0;
while (output[output_character] != '\0') { //This goes through output[] until it gets to the last written character
++output_character;
}
for (input_character = 0; input[input_character] != '\0' && output_character < (MAXCHAR * MAXLINE - 1); ++input_character) {
if (input[input_character] == ' ' || input[input_character] == '\t') {
storage[storage_character] = input[input_character]; //The array storage[] will store me the trailing blanks and tabs
++storage_character;
}
else if (input[input_character] == '\n') { //If there is a newline character and nothing has been copied into output, then it is a blank line and is not copied into output
if (output[output_character] == '\0') {
;
}
else {
output[output_character] = input[input_character]; //Copy the newline character
++output_character;
}
}
else {
storage[storage_character] = '\0';
for (storage_character = 0; storage[storage_character] != '\0'; ++storage_character) {
output[output_character] = storage[storage_character]; //If there is a character after blanks/tabs, copy storage into output
++output_character;
}
output[output_character] = input[input_character]; //Copy the character
++output_character;
storage_character = 0;
}
}
output[output_character] = '\0';
}
And the reverse function:
void reverse(char reversed[], char original[])
{
int size_original, output_char;
for (size_original = 0; original[size_original + 1] != '\n'; ++size_original) {
;
}
for (output_char = 0; size_original >= 0; ++output_char) {
reversed[output_char] = original[size_original];
--size_original;
}
reversed[output_char] = '\n';
reversed[output_char + 1] = '\0';
}
I hope this was useful. Feel free to comment if it didn't help and I will try to answer you.