Question

In: Computer Science

C++... How do I separate a C String, entered by a user, by commas? Say they...

C++... How do I separate a C String, entered by a user, by commas? Say they enter Frog,Wolf,Spider... I want a array containing the elements {Frog, Wolf, Spider}

thanks

Here's my idea so far...

cout << "Enter the column names (c of them), each name seperated by a comma" << endl;

char colNames[100];

cin >> colNames;

for (int i = 0; i < 99; ++i){

if (colNames[i] == ','){

//parse the string, assign that string name to that column

}

}

cout << colNames << endl;

Solutions

Expert Solution

# Note -> if you dont know vector please learn it, it will make your life easy w.r.t c++. if you still want me to do it for array please let me know

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
   // to store input paragraph
   string paragraph;
   // to store the final word
   vector<string> wordArray;
   // to store temp word
   string word = "";

   // take input
   cout<<"Enter your string : ";
   cin>>paragraph;

   // scan entire input
   for (int i = 0; i < paragraph.length(); ++i)
   {
       // if comma found
       if (paragraph[i] == ',')
       {
           // if word is not empty
           if (word != "")
           {
               // store word and reset it to empty
               wordArray.push_back(word);
               word = "";
           }
       } else {
           // else add new character to word
           word += paragraph[i];
       }
   }
   // for final word -> when no comma at end of paragraph
   if (word != "")
   {
       wordArray.push_back(word);
       word = "";
   }
  
   // print stored word
   cout<<"Output: { ";
   for (int i = 0; i < wordArray.size(); i++) {
cout<<wordArray[i]<<" ";
   }
   cout<<"}"<<endl;
   return 0;
}


Related Solutions

How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
c++ How do I get it before a particular string when I use <ifstream>? For example,...
c++ How do I get it before a particular string when I use <ifstream>? For example, when it's 'Apple: fruit', I only want to get the previous ':' text (Apple). And then I want to move on to the next row. How can i do?
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
C++ program that will ask the user for how many test scores will be entered. Setup...
C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name and midterm score Print out the completed test scores to a file (midTermScores.txt) . Print out list of students names and grades in the print out, a letter grade should replace numeric score using standard grading (a = 90 – 100, b=80-90, c=70-80, d=60-70, f=below 60)
Using the provided dictionary, develop an encryption algorithm that will convert a user-entered string into an...
Using the provided dictionary, develop an encryption algorithm that will convert a user-entered string into an encrypted string. Print the user inputted text and the corresponding encrypted text. cipher = {"A": "T", "B": "D", "C": "L","D": "O", "E": "F","F": "A", \ "G": "G","H": "J", "I": "K", "J": "R", "K": "I","L": "C", "M": "V", \ "N": "P", "O": "W","P": "U", "Q": "X", "R": "Y", "S": "B","T": "E", \ "U": "Z", "V": "Q", "W": "S","X": "N", "Y": "M", "Z": "H"} b) Create...
C Programming use notes Start by asking the user how many grades are to be entered....
C Programming use notes Start by asking the user how many grades are to be entered. Then ask for each one of theses grades. Calculate the average grade and the number of failing grades (grade less than 70). Display on the screen the average grade and the number of failing grades
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT