Advertisement
Porr011

Lesson 11 checkpoint

May 12th, 2019
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string word = "", words = "''";
  9.     //seting  all string variables
  10.     int length=1, a=0, count=0;        
  11.     //seting all integer variables
  12.    
  13.     cout << "Please enter a string of words separated by a comma" << endl;  
  14.     //displays text
  15.     getline(cin,words);
  16.     //gets the string of words from the user
  17.    
  18.     //For loop that finds ecah coma
  19.     for (int i=0; i<words.length(); i++)
  20.     {          
  21.         a=words[i]; // turning string into 1 dimesional array                              
  22.         if (a==44)
  23.         {  // using 44 bceause its the numbers for ASCII                                
  24.             length++;                              
  25.         } //reads character by character and then checks if the chacter is a coma
  26.         // with this info it adds one to the length of the array
  27.     } // for statement ends here
  28.    
  29.     int placeComma[length];                  
  30.     string displayWords[length];
  31.     // varibles for telling the position of commas in the string
  32.    
  33.     //for loop that finds the position of the commas
  34.     for (int i=0; i<=words.length(); i++)
  35.     {          
  36.         a=words[i];                                  
  37.         if (a==44)
  38.         {                            
  39.             placeComma[count]=i;                    
  40.             count++;                            
  41.         }
  42.         //this part saves the spot of the comma in the string
  43.         if (i==words.length())
  44.         {                    
  45.             placeComma[count]=i;                    
  46.         } // if statements checks if the character is a comma
  47.     } // for statements
  48.    
  49.     //For loop that saves the word untill the coma
  50.     // the for loop also checks for each chacter that is a comma
  51.     for(int i=0; i<length; ++i)
  52.     {                    
  53.         for (int x=0; x<=words.length(); ++x)
  54.         {      
  55.             if (x==placeComma[i])
  56.             {                  
  57.                 displayWords[i]=word;              
  58.                 word="'',''";                            
  59.                 ++i;      
  60.                 //ads one to the array
  61.                 //resets the word variable
  62.             }
  63.             else
  64.             //else statement that if doesnt goes as planed it saves each character into an integer
  65.             {
  66.                 a=words[x];                        
  67.                 if (x==placeComma[i-1]+1)
  68.                 {          
  69.                 //checks if there is a space after coma
  70.                
  71.                 // if statement that says if the character is not a space it adds the chacter to the words
  72.                     if (a!=32){                    
  73.                         word+=words[x];            
  74.                     }
  75.                 }
  76.                 //else statement that says if the character doesnt comes after a coma
  77.                 else
  78.                 {                              
  79.                     word+=words[x];                
  80.                 }
  81.             }
  82.         }
  83.     } // for loop ends here
  84.    
  85.     //for each loop that displays the array
  86.     for (string i: displayWords)
  87.     {    
  88.         cout << i << endl;  
  89.    
  90.     }
  91.     //displays each character of the array
  92.     return 0;
  93. } // main function ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement