tracy777_
asked on
formatting a string
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
I would like to know how to format a string of text in C++
for example if I typed the following:
television+video+telephone
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 );
}
}
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 );
}
}
ASKER
Thanks for the solution.
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.
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.
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+telephon e+cassette recorder+";
std::replace_copy(data,dat a+strlen(d ata),data, '+','\n');
printf("%s\n", data);
system("pause");
return 0;
}
Example:
#include <algorithm>
int main(int, char*)
{
char data[] = "television+video+telephon
std::replace_copy(data,dat
printf("%s\n", data);
system("pause");
return 0;
}
Correction, use the replace function, even simpler.
Example:
char data[] = "television+video+telephon e+cassette recorder+";
std::replace(data,data+str len(data), '+','\n');
Example:
char data[] = "television+video+telephon
std::replace(data,data+str
#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;
}