×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

A question about strings... (Beginner)

A question about strings... (Beginner)

A question about strings... (Beginner)

(OP)
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.

RE: A question about strings... (Beginner)

Or if you enter from the keyboard...

#include<stdio.h>

void main()
{
char str[50];
gets(str);
printf("%s",str);
}

RE: A question about strings... (Beginner)

(OP)
How can I reassign a string?  Everytime I try to, I get a "char to *char" 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 "Hello World" to something else like "Wacky" 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.

RE: A question about strings... (Beginner)

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:

char str1[10], str2[10];
str1 = "Hi there";
str2 = str1;



If you want to use C++ string you will have to use string class. In Borland 5.02 I think 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:

string str1[10], str2[10];
str1 = "Hi there";
str2 = str1;


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
www.ivcusa.com

RE: A question about strings... (Beginner)

Hi,

Below is a small sample of string manipulation.

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

RE: A question about strings... (Beginner)

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] = "Hello world!";  //string initialization only possible at declaration
   char string2[SIZE];
   char string3[SIZE];
   char string4[SIZE];
   int  index;

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

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

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

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

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

   return 0;
}

Hope this helps.

Happy programming!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close