I would like the output of this string to display each word in a column and ignore all punctuation. I have tried including an if statement that tells the string to start on a new line when a space is encountered but I haven't had any luck. Any hints are most welcome.

#include <iostream>
using namespace std;

int main()
{   
   const int SIZE = 257;        
   char line[SIZE];                                      
   char element;               
   int length = 16;  
    
   cout << "Enter a string (up to 256 characters): ";
   cin.getline(line, SIZE);   
   
   char* str1 = line;
   
   cout << "The sentence you entered is:\n";
    
   for (int j=0; j<strlen(str1); j++)
   cout << str1[j] << endl; 
   
cout << endl << "\n";
system("PAUSE");
return 0;
}

Recommended Answers

All 2 Replies

I would like the output of this string to display each word in a column and ignore all punctuation. I have tried including an if statement that tells the string to start on a new line when a space is encountered but I haven't had any luck. Any hints are most welcome.

I don't see any attempt to do what you want. All you did is output the line character by character and never tried to output a newline.

I don't see any attempt to do what you want. All you did is output the line character by character and never tried to output a newline.

I apologize. Here is what I have so far. I have managed to get the program to start to do what I would like. I am hoping to figure out the rest of the algorithm and if I do I will mark the post as solved. I would appreciate a hint if you find that it hasn't been solved for some time.

#include <iostream>
using namespace std;

int main()
{ 
   void sentSlice(char []); // Function prototype 

   const int SIZE = 257;  // Array size       
   char line[SIZE];       // To hold the user's sentence                                                          
   
   // Get string from user    
   cout << "Enter a string (up to 256 characters): ";
   cin.getline(line, SIZE);
   
   // Store string in pointer array
   char* str1 = line;
   sentSlice(str1);
   
   // Display original sentence in column
   cout << str1;
   
cout << endl << "\n";
system("PAUSE");
return 0;
}  

//******************************************************************
// Definition of function sentSlice. This function accepts a       *
// character array as its argument. It scanes the array looking    * 
// for a space. When it finds one, it executes a vertical shift.   * 
//******************************************************************

void sentSlice(char userSent[])
{
    int count = 0;  // Loop counter
    
    // Locate the first space, or the null terminator if there
    // are no spaces.
    while (userSent[count] != ' ' && userSent[count] != '\0')
        count++;
    
    // If a space was found, replace it with a null terminator.
    if (userSent[count] == ' ')
        userSent[count] = '\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.