c++ how to split array element - arrays

I have an array with one element, words in array are separated by tab (tab-key):
cli::array<String^> ^ PnkFld = {"Good bye cruel world"};
and program output should look like this with words being split in separate elements:
cli::array<String^> ^ PnkFld = {"Good","bye","cruel","world"};

You want the String::Split method. If you don't specify any parameters, you'll get this overload, which splits on whitespace when the parameter contains no characters.

Related

How to get specific adjacent characters of a string and add it to an another string in C

I have an abstract concept to use a for-loop to get specific adjacent characters of a string and add it to a stack, basically take all the constants of a string expression and add it to the stack separately; For example string: "1111+(2222-3333)" would be put in to the stack like "1111","2222", and "3333". I have a code below to start with that abstract concept but first trying it out with a simple one, and unfortunately it doesn't output the desired result of copying the specific adjacent characters.
char expression[20]={""},sub[20]={""};
scanf("%[^\n]%*c",&expression);
sub[15]=expression[15];
sub[16]=expression[16];
sub[17]=expression[17];
sub[18]=expression[18];
printf("%c %c %c %c\n",sub[15],sub[16],sub[17],sub[18]); //to check if copied successfully
printf("sub= %s",sub); //doesnt print expected output
After this declaration
char expression[20]={""},sub[20]={""};
all elements of the array sub contain zeroes.
You changed elements of the array starting from the position 15
sub[15]=expression[15];
sub[16]=expression[16];
sub[17]=expression[17];
sub[18]=expression[18];
The elements before the position still store zeroes.
So this call of printf
printf("sub= %s",sub);
assumes that the array contains an empty string because it first character is the terminating zero character '\0'.
Instead you could write
printf("sub= %s",sub + 15 );
Or you could change the assignments like
sub[0]=expression[15];
sub[1]=expression[16];
sub[2]=expression[17];
sub[3]=expression[18];
and then use
printf("sub= %s",sub);
Pay attention to that the second argument of this call of scanf
scanf("%[^\n]%*c",&expression);
is incorrect. You have to write
scanf("%[^\n]%*c",expression);

Word Replace in C, not Substring

Is there a way to replace all occurrences of a word in a string in C with another word. By word, I don't mean substring.
Here's what I want to achieve:
Input String:
OneOne One One OneOneOne One
Word to find:
One
Word to Replace it with:
Forty
Desired Output:
OneOne Forty Forty OneOneOne Forty
there are many example functions for replacing words in a string, i.e. What is the function to replace string in C?
you can use these functions to do what you want because the (white)spaces are also characters, see Removing Spaces from a String in C? and https://www.cs.tut.fi/~jkorpela/chars/spaces.html
so in the replace functions it is a difference if the replace string is
replace ='One' or replace = ' One '
if you use the second this should work
https://en.wikipedia.org/wiki/Whitespace_character
Unicode stored in C char

How do i count the number of elements on a given line of a file?

Something along the line of:
int n = sscanf(%s %s ...
it will save how many string element on a given line in int n,
the file can contain many lines with each line having many elements e.g.
efefefef efefef
dfefe fefef eef efef efef efefef
efefef efefef efefe
efeefef [wdfefefef]
first line n = 2 ;
second line n = 6
also how should i remove brackets from strings to get thr string inside the bracket?
regards
Presuming by 'element' you mean word, your best bet is to use strtok and pass it an appropriate list of terminators (which would include space, '[', ']', etc.

Trying to understand context in arrays/list in Perl

I am trying to understand the context (array/list/scalar) in Perl.
I tried the following:
#array = qw (john bill george);
print #array;
print "\n";
#sorted = sort (array);
print #sorted;
Why does the print #array concats the quoted words? I need to print
"#array"; to print the list? I mean # signifies an array right?
So why are the quoted needed for print?
Why does the print #sorted; prints array? If it is treated as a
scalar shouldn't it print 3 which is the size of the array?
print #sorted prints "array" because you forgot the # in the previous line :P
Change sort(array) to sort(#array) and it will print "billgeorgejohn".
As for why does print #array concatenate the quoted words, first let's make sure we're on the same page regarding qw:
#array = qw(john bill george);
is equivalent to
#array = ("john", "bill", "george");
so you're getting an array of three elements. Next, see the documentation for print. Passing a list of stuff to print will print them all, in order, separated by whatever value $, (the output field separator) has at the time. The default is empty string.
So you could do:
$, = " ";
print #array;
to get "john bill george".
The function print takes a list of arguments and prints those arguments.
If you explicitly pass a list to print then I hope you're not surprised when it prints out the elements of that list without spaces between them.
print 'one', 'two', 'three'; # prints "onetwothree
Passing an array to print is exactly the same. The contents of the array are converted to a list and then passed to print;
my #array = qw(one two three);
print #array; # prints "onetwothree"
In both of those cases, print receives three arguments and prints those arguments with nothing separating them.
Actually, Perl uses the special variable $, to control what print outputs between its arguments. By default that's an empty string, but you can change it.
Now let's consider your other case.
my #array = qw(one two three);
print "#array"; # prints "one two three"
How many arguments does print get in this example? Well, it's just one, isn't it? It's a single double quoted string. And when Perl sees a double quoted string it expands any variables in the string. The result of that expansion is then passed to print which then prints it. So we need to find out how Perl expands arrays in double-quoted. That's defined in the perldata manual page.
Array Interpolation
Arrays and slices are interpolated into double-quoted strings by
joining the elements with the delimiter specified in the $" variable
($LIST_SEPARATOR if "use English;" is specified), space by default.
So, by default, Perl interpolates arrays into double-quoted strings by inserting spaces between the elements. you can change that behaviour by changing the value of $".
These two examples might look the same, but they are actually very different.

Parsing a line in C

I have a file, in which each line contains several words that are separated by variable amount of whitespace characters (spaces and tabs). For example:
do that param1 param2 param3
do this param1
(The number of words in a line is unknown in advance and is unbounded)
I'm looking for a way to parse such a line in plain C, so that I'll have a pointer to string containing the first word, a pointer to a string containing the second word, and a pointer to a string containing everything else (that is - all of the line, except the first two words). The idea is that the "rest of the line" string will be further parsed by a callback function, determined by the first two words).
Getting the first two words is easy enough (a simple sscanf), but I have no idea how to get the "rest of the line" pointer (As sscanf stops at whitespace, and I don't know the amount of whitespace before the first word, and between the first and the second word).
Any idea will be greatly appreciated.
You can use sscanf for the rest of the line as well. You just use a "scanset" conversion instead of a string conversion:
char word1[256], word2[256], remainder[1024];
sscanf(input_line, "%255s %255s %1023[^\n]", word1, word2, remainder);

Resources