Regexreplace - How to remove newlines located between parentheses? - regexp-replace

I have a string of code that looks like
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
}
I want to run a Perl-compatible regex-replace to transform this code into
int main() {double first, second, temp;printf("Enter first number: ");scanf("%lf", &first);printf("Enter second number: ");scanf("%lf", &second);}
I've tried \n(?=[^{}]*}) but it only removes the new line preceding the {. Note that I do not want to remove any spaces or anything else. I only want to remove the new lines that are between the curly brackets.

Related

Program isn't printing first digits of two strings

Hi I'm learning C through the Modern Approach book. For this program, we just need to input a first name and last name, and the program should return Last Name, First Initial.
char *first [255];
char *last [255];
printf("Enter a first name and a last name: ");
while (getchar() == ' ');
scanf("%s", first);
while (getchar() == ' ');
scanf("%s", last);
while (getchar() == ' ');
char firstInitial = (char) first[0];
printf("%s, ", last);
putchar(firstInitial);
When I run it, it doesn't print the first two characters.
e.g
Enter a first name and a last name: Aaron Smith
mith, a
This will get you started with getting the the first name.
#include <stdio.h>
int main() {
// Create a string
char firstName[255];
// Ask the user to input some text
printf("Enter your first name: \n");
// Get and save the text
scanf("%s", firstName);
// Output the text
printf("first name %s. 1st char %c", firstName, firstName[0]);
return 0;
}
#include <stdio.h>
void main()
{
char a = getchar();
char b;
scanf("%c",&b);
printf("%c",b);
}
Run the above code and see the result for yourself.
you can see that the getchar() function terminates only after when a “enter” key is pressed.
this causes an additional ‘\n’ character along with the single character you entered, to be in the input queue.
so character ‘b’ is assigned ‘\n’ and the compiler does not prompt you to provide input for ‘b’.

trying the check if cin.get() leaves the end of line character in stream

1.i am trying to check whether the cin.get() leaves the end line character in stream and considered it for next input.
i have tried this code in code blocks but unable to provide input for next string,i am attaching code i have tried and the output .could anyone please explain.
#include<iostream>
using namespace std;
int main()
{
char s1[10];
char s2[10];
cout << "enter the first string: ";
cin.get(s1, 10);
cout << "enter the second string: ";
cin.getline(s2, 10);
cout << s1 << " " << s2;
return 0;
}
enter the first string: hello
enter the second string: hello
please explain the output
This get function reference says that your overload is
Same as get(s, count, widen('\n'))
And that overload of the function reads until (among other things)
the next available input character c equals delim, as determined by Traits::eq(c, delim). This character is not extracted (unlike basic_istream::getline())
[Emphasis mine]
So the newline is left in the input buffer, for the getline call to read as an "empty" line.
If you want to read lines, I suggest you use std::string and std::getline (which does read, and throw away, the newline).
cin.get() grabs the newline character by default. It will not leave the newline in the stream.

8051 sentence and word counter

I found this code below on the internet which is suppose to count the sentences on an 8051 MCU.
Can someone please explain to me what is exactly happening where there are question marks.
Any kind of help would be highly appreciated.
#include<string.h>
char code *text=" what is a program? that has, a a lot of errors! When " ;
char code *text1=" you compile. this file, uVision. reports a number of? ";
char code *text2=" problems that you! may interactively correct. " ; //Null characters are also included in array!!!
void count ( char pdata* , char pdata*);
void main (void){
char pdata Nw,Ns;
char data TextNw[2],TextNs[2];
count(&Nw, &Ns); // call subroutine
TextNw[0]=Nw/10; //?????????????????????????????????
TextNw[1]=Nw%10; //?????????????????????????????????
TextNs[0]=Ns/10; //?????????????????????????????????
TextNs[1]=Ns%10; //?????????????????????????????????
while(1);
}
void count ( char pdata *Nw, char pdata *Ns ){
unsigned char N, i, ch;
typedef enum {idle1, idle2} state; //?????????????????????????????????
state S; // begining state
P2=0x00; // pdata bank definition it must be performed first!!
*Ns=*Nw=0; // without proper start-up there is no initialisation, initialise now!!
S=idle1; // beginning state
N=strlen(text)+strlen(text1)+strlen(text2)+3; //????????????? + 3 to acount 3 Null characters!
P2=0x00; // pdata bank definition
for(i=0;i!=N;i++){
ch=text[i]; // take a caharacter from the text
switch (S)
{
case (idle1):{
if (ch==0) break; // skip NULL terminating character!
if (ch!=' '){
S=idle2;
(*Nw)++;
}
break;
}
case(idle2):{
if (ch==0) break; // skip NULL terminating character!
if((ch==' ')||(ch==','))S=idle1;
else if ((ch=='?')||(ch=='.')||(ch=='!')){
S=idle1;
(*Ns)++;
}
break;
}
}
}
}
This program does 2 things in conjunction - counts number of sentences in the text and counts the number of words in the text. Once the counting is done, the results are stored in 2-char arrays. For example, for 57 words in 3 sentences the results will be stored as this: TextNw = {'5','7'} and TextNs = {'0','3'}.
The variable N contains the full length of the text with the addition of 3 null terminating characters (one per sentence).
The algorithm simultaneously counts words and sentences. In idle1 state the counting is in word-counting mode. In idle2 state the counting is in sentence-counting mode. The modes are interchanged according to current character being read - if delimiter is encountered, the appropriate counter is increased.

Read lines from file into string vector? C++

Ok, I'm a rookie at this and here is what I've been sitting here for a while scratching my head doing.
My goal is to read in a file from a command line argument and store the contents of the file in an array strings of which each element is a line from the file. I need the whole line including white spaces. And I need to cycle through the whole text file without knowing how large/small it is.
I'm fairly sure that key.eof() here is not right, but I've tried so many things now that I need to ask for help because I feel like I'm getting further and further from the solution.
ifstream key(argv[2]);
if (!key) // if file doesn't exist, EXIT
{
cout << "Could not open the key file!\n";
return EXIT_FAILURE;
}
else
{
vector<string> lines;
for (unsigned i = 0; i != key.eof(); ++i)
{
getline(key, lines[i]);
}
for (auto x : lines)
cout << x;
If anyone could point me in the right direction, this is just the begging of what I have to do and if feel clueless. The goal is for me to be able to break down each line into a vector(or whatever I need) of chars INCLUDING white spaces.
I think you want something like this:
vector<string> lines;
string line;
while(getline(key, line)) // keep going until eof or error
lines.push_back(line); // add line to lines
To continuous read lines from file and keep it in array, I would do something like this using a while loop instead of a for loop.
int counter = 0;
while (getline(key, lines[counter]) {
counter++;
}

Extra whitespace in output after using scanf ( %c)

After I run this do/while loop for the first time the output always seems to have an extra space before the printf. I think it might have something to do with buffer overflow, but I'm not quite sure how to get rid of it.
Any help would be much appreciated, thanks!
do {
printf("Enter a format specifier: ");
scanf(" %c", fmt+1);
printf(fmt, value);
} while(*(fmt+1) != 'q');
**fmt is a char array.
I expanded your code into a complete program as follows:
#include "stdio.h"
main () {
char fmt[2] = "% ";
int value = 23;
do {
printf("Enter a format specifier: ");
scanf(" %c", fmt+1);
printf(fmt,value);
} while(*(fmt+1) != 'q');
}
... and got the following result:
Enter a format specifier: i
23h (Enter a format specifier: d
23h (Enter a format specifier: x
17h (Enter a format specifier: o
27h (Enter a format specifier: q
qh (
This output was a little different from what you seem to have observed, but I think it might be the same in principle. If so, I think it is as you suspected: a buffer overflow error caused by the fact that fmt is declared as a char array that isn't long enough to include the null that should be at the end of any C string. Thus, printf() kept on writing random data after the format character until it happened to come across a null.
Changing the fourth line of code to ensure that the string has enough space for the visible characters and the terminating null, but no more, i.e.:
char fmt[] = "% ";
... gives the following result (which was, perhaps, something like you expected?):
Enter a format specifier: i
23Enter a format specifier: d
23Enter a format specifier: x
17Enter a format specifier: o
27Enter a format specifier: q
q
For more info on this issue you could read Char array initialization dilemma.

Resources