How to handle empty space on enter key press in flex? - c

I am trying to create a pattern like below, for ,when enter button is pressed twice, the program should terminate, but if user enters something in the line, the program shouldn't terminate.
[\n\n] {
if( strcmp(yytext,"?")){
return 0;
}
}
I try to check with strcmp for yytex is null space, but what should ı put inside " " ?

[\n\n] doesn't match two consecutive line breaks. It's a character class matching a single character that's either a line break or ... a line break. It's equivalent to just \n - duplicate elements in a character class have no effect.
To match two consecutive line breaks, what you want is \n\n without the bracket.
As for strcmp, there is absolutely no reason to check the contents of yytext here. Using the pattern [\n\n], yytext can only ever be equal to "\n" and using \n\n, it can only ever be equal to "\n\n". Either way there's no reason to compare it to anything when you already know what its value is going to be.

This is the solution for this situation guys
^\n yyterminate();

Related

How can i read certain parts of of a text file and store them in character arrays to be compared?

here is the part of the program that i am struggling with, "You will need some kind of loop to read through the entries in a text file, for the 1st 4 fields in the text file, you will know that you are at the end of the field when you read a comma. For the last field in the text file, you will know you are at the end of the field when you read a newline.
I did the program using functions from string.h but was challenged do the same program without the use of string.h and i am stuck at the loop part of this program.
i know my loop is incorrect but i am having trouble figuring out the correct loop to use, any tips will be helpfully
do
{
ch = fgetc(fout);
if(ch == EOF)
{
break;
}
field[x]=ch;//loads all the characters into the array
printf("%c",field[x]);//printing
x++;
}while(1);//infinite loop
fclose(fout);
One way to solve this would be to make a 2dimensional array so a array of field and have an if in the loop that increments the variable that says which field array to use when there is a comma. You would end up with 5 char arrays that each have one field, and you'd have a \n in the last one Wich you then need to remove.
Hope this helps

C while loop until user enters "quit"

Here's my code:
1. User types in two names, with a space in between. This means that two strings need to be read. I.e. input:
John Doe.
The strings are then checked in a char-array. (works fine).
The while loop goes on until the user types "stop" - only "stop".
How can I make it to stop directly if "stop" is entered - without the need to check the second string?
The code:
while(bool==false)
{
scanf("%20s%20s", name1, name2);
if(strcmp(name1, "stop")==0)
{
break;
}
// but still the second name has to be entered
rest of code...
}
Thanks for any tips!
I suggest you use fgets to get the input, check for the "stop" string, and then use sscanf to parse the input.
You can put to use the regular expression character class support provided by scanf.
You could do:
scanf("%s%[^\n]s", name, temp);
Here, your first word is mandatory while second is optional.
When you input 2 words, your temp would have a leading space.
If you want to directly avoid it, you can do so by:
char *p = temp;
scanf("%s%[^\n]s", name, p++);
Here, you can later access your 2 words using name and p

Code to replace all the tabs and if there are more than 1 space by a single space?

This question is in K&R, exercise 1.9. I wrote the following code:
#include<stdio.h>
main()
{
int c,i=0,n=0;
while((c=getchar())!=EOF)
{
if(c!=' '||c!='\t')
{
i=0;
putchar(c);
}
else if(c==' '||c=='\t')
{
i++
}
if((c+1)!=' '||(c+1)!='\t')
n=i;
if(n!=0)
{
c=' ';
putchar(c);
}
}
}
but i could not get the desired output. I am using gcc in ubuntu. When I enter something like hello\t\ta as input then my output is hello\_\_a i.e number of tab is replaced by number of space and when I enter hello\_\_a then my output is same as input.
Please help me with it or suggest me something new to get the desired output.
Instead of giving your the full working program, I prefer to guide you to the right direction.
First of all, c+1 does not mean "next character in the input". It only adds 1 to the value of c, which effectively converts c to the next character in the ASCII table.
For example if c is 'a', c+1 means 'b', which is next character int the ASCII table, and if c is ' ' (a single space) that has a code of 32 in the table, c+1 is '!' that has a code 33 in the table.
Well, to get the next character, you need to read it! In the same way you read the first character. The best way to achieve this, is to always hold the previous read character, and check that with the currently read character.
So you need two variables, for example c and pc. You read the character and store it in c. At first, pc is '\0'. If the read character is not space or tab, you write it to the output. If it is tab, you change it to space. And if it is space, you check the previous character (pc). If it is not space, print c. At the end of the loop, you should store the value of c into pc, which means you are holding the previous character in pc.
I guess I told you the complete solution!
The problem is: you want to check the NEXT character, but you check the current character's value incremented by one.
The approach is slightly wrong, here is a hint, keep the last character as state, if the newly entered character is a space and the last character was a space, then don't output, simply go back round the loop and wait for the next character.
If the current character is not a space, output and update the state...

How to interpret special characters in command line argument in C?

First problem:
Suppose we write a simple program which takes in command line arguments and prints to a file. If the user enters
writetofile Hello!0\n w%orl\t!##y
bash replies with
!0: event not
found.
Without the user knowing things like using quotes ('') or escape characters ('\'), how do I handle this stuff instead of bash understanding it as a command?
Second problem:
Once I get these arguments, how do I interpret them as special characters and not sequences of characters. (ie. \t is tab, not '\''t')
That is, how do make sure that the program writes this to the file:
Hello!0
w%orl !##y
and not
Hello!0\n w%orl\t!##y
You can't get bash to stop parsing a command line as a command line. In your particular case, set +H would disable history expansion, thus allowing !0 as a literal, but a command would still fail if you included (parenthesis), $variables, #comments, ampersand & and a dozen other elements of shell syntax.
Note that this only applies to the parsing stage of the shell, not for any data that is passed literally into execve or similar.
Either make the user quote the data ('single quotes' will take care of everything except other single quotes), or make your program read the data itself so that the interaction goes
$ writetofile
Please enter your gibberish and press enter: Hello!0\n w%orl\t!##y
Thank you for your eloquent contribution.
$
Regarding the second problem: as #Jims said, you could use printf(1) to print the string. Good idea, but be aware that that would work only because the escapes you (appear to) want to recognise, like \t and \n, are the same as the ones that printf recognises.
These escapes are common, but there's nothing fundamental about them, so the general answer to your question – and the answer if you want to recognise an escape that printf doesn't – is for your program to interpret them. The C-like code to do that would be something like:
char *arg = "foo\\tbar\\n"; /* the doubled backslashes are to make this valid C */
int arglen = strlen(arg);
for (i=0; i<arglen; i++) {
if (arg[i] == '\\') { // we've spotted an escape
// interpret a backslash escape
i++; // we should check for EOS here...
switch (arg[i]) {
case 'n': putchar('\n'); break;
case 't': putchar('\t'); break;
// etc
}
} else {
// ordinary character: just output it
putchar(arg[i]);
}
}
(insert error handling and good style, to taste).
First problem: Use single quotes. ie: writetofile 'Hello!0\n w%orl\t!##y'
Second problem: Use printf.

Reading strings in C

I have to read strings of different length in C.I can do it using an array of pointers and using ,alloc inside the loop.The problem is that the input to the program is a series of sentences like this:
Rene Decartes once said,
"I think, therefore I am."
and there can be many more. How can I check programmaticaly that the user has finished giving the input sentences.
Maybe he's even looking for the end-of-file (end of input, end of stream). You can detect the end of the input stream by checking the return value of various input functions. You did not tell us which function you use for reading input. E.g. with fgets() and input on standard input you would do
#include <stdio.h>
#define BUFFERSIZE 256
char buffer[BUFFERSIZE];
...
while (fgets (buffer, sizeof buffer, stdin) != NULL) {
/* Do something with buffer. */
}
/* No more lines on stdin. */
If I understand the question, you're not looking for the "end of line" signal; you're looking for the "end of sentences" signal.
A common way to do this is to ask the user to enter an empty line when s/he's done. Then (depending on how you're reading the strings), you can check for either an empty string, or a string with only a single (newline) character.
Loop the array keys until you find a "\0" character.
Make use of some delimiters and ask the user to enter it on finishing all the sentences,,
Also in-order to check for the end of each line check with '\n' as the delimiter,,
You can tell the user to type in a special symbol or string which will indicate that the input has ended. For example you can tell the user to enter a . in a new line to terminate the input. Checkout #Adam Liss 's answer also.
You should process them one line at a time using "end of line" as the delimiter. You can store those into a **char -- properly allocated of course. Do be careful of boundaries, and prefer strncpy() to strcpy() -- the later is unsafe.

Resources