Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- using namespace std;
- int main()
- {
- string word = "", words = "''";
- //seting all string variables
- int length=1, a=0, count=0;
- //seting all integer variables
- cout << "Please enter a string of words separated by a comma" << endl;
- //displays text
- getline(cin,words);
- //gets the string of words from the user
- //For loop that finds ecah coma
- for (int i=0; i<words.length(); i++)
- {
- a=words[i]; // turning string into 1 dimesional array
- if (a==44)
- { // using 44 bceause its the numbers for ASCII
- length++;
- } //reads character by character and then checks if the chacter is a coma
- // with this info it adds one to the length of the array
- } // for statement ends here
- int placeComma[length];
- string displayWords[length];
- // varibles for telling the position of commas in the string
- //for loop that finds the position of the commas
- for (int i=0; i<=words.length(); i++)
- {
- a=words[i];
- if (a==44)
- {
- placeComma[count]=i;
- count++;
- }
- //this part saves the spot of the comma in the string
- if (i==words.length())
- {
- placeComma[count]=i;
- } // if statements checks if the character is a comma
- } // for statements
- //For loop that saves the word untill the coma
- // the for loop also checks for each chacter that is a comma
- for(int i=0; i<length; ++i)
- {
- for (int x=0; x<=words.length(); ++x)
- {
- if (x==placeComma[i])
- {
- displayWords[i]=word;
- word="'',''";
- ++i;
- //ads one to the array
- //resets the word variable
- }
- else
- //else statement that if doesnt goes as planed it saves each character into an integer
- {
- a=words[x];
- if (x==placeComma[i-1]+1)
- {
- //checks if there is a space after coma
- // if statement that says if the character is not a space it adds the chacter to the words
- if (a!=32){
- word+=words[x];
- }
- }
- //else statement that says if the character doesnt comes after a coma
- else
- {
- word+=words[x];
- }
- }
- }
- } // for loop ends here
- //for each loop that displays the array
- for (string i: displayWords)
- {
- cout << i << endl;
- }
- //displays each character of the array
- return 0;
- } // main function ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement