C Parsing and storing data read in from commandline argument
C++ Parsing and storing data read in from commandline argument.
My question is regarding parsing & storing of Strings
I am supposed to read in 2 files from a commandline argument and parse through the data. Here are relative details.
- Each line is made up of a self-implemented String. While it does not support as extensive of functions as the STL string library, it can divide a string into substrings, which probably will be the main function I would need to make use of. Given this, I cannot #include string.
- I cannot use any STL container class. I am however allowed to use <fstream>, <iostream>, <cstring>
- The input data file is composed thusly; on the first line, it has number of total lines. For each line after that, it is a line of string separated by | character. I need the \'|\' character because I want to split my string into substrings. There are white spaces within a section. Here is an example.
data.txt // file name
2
apple juice|tree top|food|farm
long|short|load file|write file
I want the result to be
apple juice
tree top
food
farm
long
...
- After I parse the data, I want to store it to individual String. so from the example above, the result should be
String1 = \"apple juice\";
String2 = \"tree top\";
String3 = \"food\";
String4 = \"farm\";
String1 = \"long\"; // since there are only 4 substrings per line, once it goes to the next line it will start over and assign the data to relative container.
...
I asked this question before and got a great answer, only I forgot to mention that each section being divided into substrings can have whitespaces. Here is the code I got from the answer.
https://www.chegg.com/homework-help/questions-and-answers/question-regarding-parsing-storing-strings-supposed-read-2-files-commandline-argument-pars-q15320075
Now, when it encounters whitespace it just terminates the entire function. What can be done?
Solution
import java.util.*;
public category ArrayListExample i am attending to add String
*elements therefore I created it of string kind */
ArrayList<String> obj = new ArrayList<String>();
/*This is however components ought to be value-added to the array list*/
obj.add(\"Ajeet\");
obj.add(\"Harry\");
obj.add(\"Chaitanya\");
obj.add(\"Steve\");
obj.add(\"Anuj\");
/* Displaying array list components */
System.out.println(\"Currently the array list has following elements:\"+obj);
/*Add part at the given index*/
obj.add(0, \"Rahul\");
obj.add(1, \"Justin\");
/*Remove components from array list like this*/
obj.remove(\"Chaitanya\");
obj.remove(\"Harry\");
System.out.println(\"Current array list is:\"+obj);
/*Remove part from the given index*/
obj.remove(1);
System.out.println(\"Current array list is:\"+obj);
}
}
Output:
Currently the array list has following elements:[Ajeet, Harry, Chaitanya, Steve, Anuj]
Current array list is:[Rahul, Justin, Ajeet, Steve, Anuj]
Current array list is:[Rahul, Ajeet, Steve, Anuj]
Methods of ArrayList category
In the on top of example we\'ve got used ways like add and take away. but there square measure range of ways accessible which may be used directly exploitation object of ArrayList category. Let’s discuss few of the necessary ways.
1) add( Object o): This methodology adds Associate in Nursing object o to the arraylist.
obj.add(\"hello\");
This statement would add a string how-do-you-do within the arraylist ultimately position.
2) add(int index, Object o): It adds the item o to the array list at the given index.
obj.add(2, \"bye\");
It will add the string bye to the 2d index (3rd position because the array list starts with index 0) of array list.
3) remove(Object o): Removes the item o from the ArrayList.

