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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question about strings... (Beginner) 1

Status
Not open for further replies.

Skywise

Programmer
Apr 11, 2001
9
US
My question about strings is this:
How the heck do you make them?

I have tried everything I can think of... I am using Borland's 5.02 c++ complier.
Everything I try doesn't seem to work. Can someone show me a way to make a
simple string without getting errors galore? Thanks.
 
Or if you enter from the keyboard...

#include<stdio.h>

void main()
{
char str[50];
gets(str);
printf(&quot;%s&quot;,str);
}
 
How can I reassign a string? Everytime I try to, I get a &quot;char to *char&quot; error (or something like that). I think its trying to tell me something about pointers, but all I want to do is to change the string's content all at once (for example, change it from &quot;Hello World&quot; to something else like &quot;Wacky&quot; or something). Surely thats possible, right? (I know it is, I just dont know how to do it yet). Thanks again for all your help, Skywise.
 
If you are using character strings (arrays), you will need to use their functions, e.g. strcpy, strcat. You cannot assign a character string like this:
Code:
char str1[10], str2[10];
str1 = &quot;Hi there&quot;;
str2 = str1;
[code]


If you want to use C++ string you will have to use string class. In Borland 5.02 I [u]think[/u] they are in the CString header. I don't have that version installed any more so I can't check it. Once this has been included you can do this:
[code]
string str1[10], str2[10];
str1 = &quot;Hi there&quot;;
str2 = str1;
[code]

You best bet is to get a copy of the standard library that will work with 5.02. They used to have one at SGI's site. They you can use the standard string library.

 James P. Cottingham
[URL unfurl="true"]www.ivcusa.com[/URL]
 
Hi,

Below is a small sample of string manipulation.

#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char mystr[20]= &quot;start program&quot;; // init the string
printf(&quot;\n%s&quot;,mystr); // print the string
strcpy(mystr,&quot;Hello world&quot;); // change th string
printf(&quot;\n%s&quot;,mystr);
strcpy(mystr,&quot;Bye world&quot;); // change it again
printf(&quot;\n%s&quot;,mystr);
strcat(mystr,&quot;, BYE&quot;); // add to the string
printf(&quot;\n%s&quot;,mystr);

getch();

}

This is only a start. Take the time to look up the string functions. Most begin with
'str'. Don't forget to #include <string.h>.
Pappy
You learn something everyday.
 
In C++ there is no such thing as agregate string operations meaning you can't assign one string to another, you can't assign a string a value unless it is during declaration. You can read a string during input and you can output a string. the following example shows how to get a string at run time, how to copy one string to another, how to concatenate values to a string, and how to output a string:

#include <iostream.h> //for console i/o
#include <conio.h> //for getch()
#include <string.h> //for string manipulation

int const SIZE = 40;

int main(){
char string1[SIZE] = &quot;Hello world!&quot;; //string initialization only possible at declaration
char string2[SIZE];
char string3[SIZE];
char string4[SIZE];
int index;

cout<<&quot;Value of string1 = &quot;<<string1<<endl; //display initialized string
cout<<&quot;Please enter your first name : &quot;<<endl;
cin>>string2; //run time input of string
cout<<&quot;Value of string2 = &quot;<<string2<<endl;

//copy one string to another like strcat() the 2nd param to strcpy() can be a literal value
strcpy(string3, string1);
cout<<&quot;Value of string3 = &quot;<<string3<<endl;

//append the word including and name in string2 to the end of string3
strcat(string3, &quot; including &quot;);
strcat(string3, string2);
cout<<&quot;Value of string3 = &quot;<<string3<<endl;

//copy hello from string3 to string4
index = 0;
while(string3[index] != ' '){
string4[index] = string3[index];
index++;
}
cout<<&quot;Value of string4 = &quot;<<string4<<endl;

//pause program to view output
cout<<&quot;Press any key to exit...&quot;<<endl;
getch();

return 0;
}

Hope this helps.

Happy programming!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top