Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can You Manipulating Strings In Older C++ Versions?

Status
Not open for further replies.

SpeakerForTheDead

Programmer
Jan 31, 2002
28
0
0
US
Okay. I'm used to using Microsoft Visual C++ v6.0, but I have v4.0 at home. I'm used to adding strings the way you'd add numbers. I'm used to using string.assign() and string.find() for all of my purposes. But I can't use them any more. So I was wondering what tools for manipulating strings the old string.h header file has. So far, all I've found is strcopy(). I would appreciate any help.
 
There are a lot of fucntions for manipulate strings.
"strcat" concatenate two strings, "strcpy" copy one string to another,"strstr" find the first occurence of a string in another one etc.
For more detail information on this subject,check the link below.

 
Thanks. Most of that was very helpful. However, I can't figure out how to copy a string starting in the middle of the string. With .assign, it's easy, you just enter where you want the copying to start. I haven't found a function (or a sequence of functions) in <string.h> that will allow me to do that yet.
 
strncpy will do that for you...

char buffer[] = &quot;My name is Slim Shady&quot;;
char substr[128];
strncpy(substr,buffer[8],7);
substr[7] = 0;
substr is &quot;is Slim&quot;

Matt
 
Still not working. It compiles and executes, but it does very weird things...

Please help.

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>


void get(char prompt[100], char input[100]) //get(prompt, variable);
{//get - prompts and gets a value
cout << &quot;Enter &quot; << prompt << &quot;: &quot;;
gets(input);
}//end of get function

void get(char prompt[100], int input) //get(prompt, variable);
{//get - prompts anbd gets a value
cout << &quot;Enter &quot; << prompt << &quot;: &quot;;
cin >> input;
}//end of get function

//function prototypes
void addInitialize(char[100]);
void addInput(char[100], char[100]);
void addProcessing(char[100], char[100][100], int &);
void addOutput(char[100], char[100][100], int);


void main()
{//main - main function

//declare fstream commands
ofstream fout;
ifstream fin;

//intro
cout << &quot;Program Writer\n\n&quot;;
getch();

//declare main function variables
char filename[100] = &quot;&quot;; //name of file
char x = 'n'; //multi-use variable (character)
int y; //multi-use variable (integer)
char initialize[100][100] = {&quot;&quot;}; //initialization commands
char input[100][100] = {&quot;&quot;}; //input commands
char processing[100][100] = {&quot;&quot;}; //processsing commands
char output[100][100] = {&quot;&quot;}; //output commands
int length[4] = {0,0,0,0}; //numbers of commands {initialize, input, processing, output}

while (x != 'y' && x != 'Y')
{//create file
//gets(filename);//debug
get(&quot;Filename (not including extension)&quot;,filename);
strcat(filename, &quot;.cpp&quot;);
fin.open(filename);
if (fin.is_open() )
{//if file already exists
cout << &quot;Erase existing file? (y/n)&quot;;
cin >> x;
}//end if file already exists
else
{
cout << &quot;Huh&quot;;
x = 'y';//if file DOES NOT exist
}
}//end create file
fin.close();

//write beginning of file
fout.open(filename);
fout << &quot;//Created using Program Writer\n&quot;
<< &quot;#include <iostream>\n&quot;
<< &quot;#include <string.h>\n&quot;
<< &quot;using namespace std;\n&quot;
<< &quot;void main()\n&quot;
<< &quot;{\n\t&quot;;

main:
//main menu
system(&quot;cls&quot;);
cout << &quot;***MAIN MENU***\n&quot;
<< &quot;(1)Add Input\n&quot;
<< &quot;(2)Add Calculation\n&quot;
<< &quot;(3)Add Output\n&quot;
<< &quot;(4)Done\n\n&quot;;
get(&quot;selection&quot;,y);
switch(y)
{//add commands branch
case 1:
//add input
addInitialize(initialize[length[0]]);
addInput(input[length[1]], initialize[length[0]]);
length[0]++;
length[1]++;
cout << &quot;Input added&quot;;
getch();
goto main;

case 2:
//add calculation (processing)
addProcessing(processing[length[2]], initialize, length[0]);
length[2]++;
cout << &quot;Calculation added&quot;;
getch();
goto main;

case 3:
//add output
addOutput(output[length[3]], initialize, length[0]);
cout << &quot;Output added&quot;;
goto main;

}//end add commands branch

//write commands to file
for (y = 0; y < length[0]; y++)//write initializations
fout << &quot;\t&quot; << initialize[y] << endl;
for (y = 0; y < length[1]; y++)//write input
fout << &quot;\t&quot; << input[y] << endl;
for (y = 0; y < length[2]; y++)//write processing
fout << &quot;\t&quot; << processing[y] << endl;
for (y = 0; y < length[3]; y++)//write output
fout << &quot;\t&quot; << output[y] << endl;

//write end of file
fout << &quot;}&quot;;
cout << &quot;\nProgram completed and ready for compliling...\n&quot;;
}//end of main function


void addInitialize(char initialize[100]) //addInitialize(initialize command);
{//addInitialize - add initialization command

//declare addInitialize variables
int x = 0; //multi-use variable (integer)
char y[100] = &quot;&quot;; //multi-use variable (string)

//select variable type
cout << &quot;\nVARIABLE TYPES\n&quot;
<< &quot;(1)Integer\n&quot;
<< &quot;(2)Real Number\n&quot;
<< &quot;(3)Character\n&quot;
<< &quot;(4)String\n\n&quot;;
while (x < 1 || x > 4)
get (&quot;selection&quot;, x);
switch(x)
{//select variable type branch
case 1:
//integer
strcpy(y, &quot;int &quot;);
break;
case 2:
//real number
strcpy(y, &quot;float &quot;);
break;
case 3:
//character
strcpy(y, &quot;char &quot;);
break;
case 4:
//string
strcpy(y, &quot;char* &quot;);
}//end select variable type branch
initialize = y;//add variable type to command

gets(y);//debug
//get name of variable
get(&quot;name of variable&quot;, y);
strcat(initialize, y);//add variable name to command

//get initial value
get (&quot;initial value&quot;, y);
//add initial value to command
if (x == 1 || x == 2)
{
strcat(initialize, &quot; = &quot;);
strcat(initialize, y);
}
else if (x == 3)
{
strcat(initialize, &quot; = &quot;);
strcat(initialize, &quot;'&quot;);
strcat(initialize, y);
strcat(initialize, &quot;'&quot;);
}
else
{
strcat(initialize, &quot; = &quot;);
strcat(initialize, &quot;\&quot;&quot;);
strcat(initialize, y);
strcat(initialize, &quot;\&quot;&quot;);
}

strcat(initialize, &quot;;&quot;);//end command line

}//end of addInitialize function


void addInput(char input[100], char variable[100]) //addInput(input command, variable);
{//addInput - add input command

//declare addInput variables
char x[100] = &quot;&quot;; //multi-use variable (string)
int y = 0; //multi-use variable (integer)
while (strchr(variable+y,'=') != 0)
y++;

strncpy(variable, variable+7, y-7);

//get variable prompt
get(&quot;Variable Prompt&quot;, x);

//create input command
strcpy(input, &quot;cout << \&quot;Enter &quot;);
strcat(input, x);
strcat(input, &quot;: \&quot;;\n\t&quot;);
if (strcmp(variable, &quot;char*&quot;) == 0)
{
strcat(input,&quot;gets(&quot;);
strcat(input, variable);
strcat(input, &quot;);&quot;);
}
else
{
strcat(input, &quot;cin >> &quot;);
strcat(input, variable);
strcat(input, &quot;;&quot;);
}

}//end of addInput function


void addProcessing(char processing[100], char variables[100][100], int &len) //addProcessing(processing command, variable list, length of variable list);
{//addProcessing - add processing command (calculation)

//display list of variables
cout << &quot;AVAILABLE VARIABLES\n&quot;;
for (int i = 0; i < len; i++)
cout << &quot;(&quot; << i+1 << &quot;) &quot; << variables << endl;
cout << &quot;(&quot; << len+1 << &quot;) New Variable\n&quot;;

//select final variable
int x = 0; //multi-use variable (integer)
while (x < 1 || x > len+1)
get(&quot;Final Variable&quot;, x);
if (x == len+1)
{//create new variable
addInitialize(variables[len]);
len++;
}//end create new variable
x--;
int y = 0; //multi-use variable (integer)
while (strchr(variables[x]+y,'=') != 0)
y++;

//write beginning of processing command
strncpy(processing,variables[x]+7, y-7);
strcat(processing, &quot; = &quot;);

//get final variable
char formula[100] = &quot;&quot;; //formula to get final variable
cout << &quot;Ex. (variable1 + variable2) * variable 3\n&quot;;
gets(formula);//debug
get(&quot;formula to get final (include names of variables)&quot;,formula);

//make end of processing command
strcat(processing, formula);
strcat(processing, &quot;;\n&quot;);

}//end of addProcessing function


void addOutput(char output[100], char variables[100][100], int length)//addOutput(output command, variable list, length of variable list);
{//addOutput - add output command

//display list of variables
cout << &quot;AVAILABLE VARIABLES\n&quot;;
for (int i = 0; i < length; i++)
cout << &quot;(&quot; << i+1 << &quot;) &quot; << variables << endl;
cout << &quot;(&quot; << length+1 << &quot;) No Variable\n&quot;;
//select variable
int x = 0; //multi-use variable (integer)
while (x < 1 || x > length+1)
get(&quot;Output Variable&quot;, x);
x--;

//get comment
char y[100] = &quot;&quot;; //multi-use variable (string)
gets(y);//debug
int z = 0; //multi-use variable (integer)
while (strchr(variables[x]+z,'=') != 0)
z++;
get(&quot;Comment&quot;,y);
char a[100] = &quot;&quot;; //multi-use variable (string)

//write output command
strncpy(a,variables[x]+7, z-7);
if (x != length+1)
{
strcpy(output, &quot;cout << \&quot;&quot;);
strcat(output, y);
strcat(output, &quot;:\&quot; << &quot;);
strcat(output, a);
strcat(output, &quot;<< endl;&quot;);
}
else
{
strcpy(output,&quot;cout << \&quot;&quot;);
strcat(output, y);
strcat(output, &quot;\n\&quot;;&quot;);
}

}//end of addOutput command

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top