Solved

formatting a string

Posted on 2002-04-01
7
211 Views
Last Modified: 2010-04-02
hi,

I would like to know how to format a string of text in C++
for example if I typed the following:

television+video+telephone+cassette recorder+

The output to the screen should be:

television
video
telephone
cassette recorder

Any help much appreciated, could do it within pascal but having a problem now that I have upgraded to C++.

Tracy
x
0
Comment
Question by:tracy777_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 50 total points
ID: 6910066
#include <iostream>
using namespace std;

int main(void)
{
     static     char strData[128];
     if(!gets(strData))
          {
          return 1;
          }
     short     numLength=strlen(strData);
     for(short numLoop=0; numLoop < numLength; numLoop++)
          {
          if ('+' == strData[numLoop])
               {
               strData[numLoop]=0x0a;
               }
          }
     cout << strData;
     return 0;
}
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6910070
...and in answer to your deleted question

#include <iostream>
using namespace std;

int main(void)
{
     static     char strData[128];
     if(!gets(strData))
          {
          return 1;
          }
     short     numLength=strlen(strData);
     for(short numLoop=0; numLoop < numLength; numLoop++)
          {
          if ('=' == strData[numLoop] && ' ' ==strData[numLoop+1])
               {
               strData[numLoop]=0x0d;
               strData[numLoop+1]=0x0a;
               }
          }
     cout << strData;
     return 0;
}
0
 

Expert Comment

by:akohli
ID: 6910138
Hi ,
U can use strtok to do that..

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

void main( void )
{
   printf( "%s\n\nTokens:\n", string );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tracy777_
ID: 6910144
Thanks for the solution.

0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6910190
Yes, akohli, you can use strtok(), but there are other things you have to do depending on your use of it.
If you want to leave it as one string with the CR/LF in it, you'll have to piece it back together.  There's nothing wrong with that.  In most cases, I would use strtok().  For this example, I took an easy route.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6910791
For a one to one replacement, a simpler method is ot use the replace_copy function.
Example:
#include <algorithm>

int main(int, char*)
{
     char data[] = "television+video+telephone+cassette recorder+";
     std::replace_copy(data,data+strlen(data),data,'+','\n');
     printf("%s\n", data);

     system("pause");
     return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 6910799
Correction, use the replace function, even simpler.

Example:
char data[] = "television+video+telephone+cassette recorder+";
std::replace(data,data+strlen(data), '+','\n');
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question