CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

Define string in C++ topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 02-02-2012, 09:05 AM
Junior Member
 
Join Date: Feb 2012
Posts: 28
Thanks: 4
Thanked 0 Times in 0 Posts
ballurohit is on a distinguished road
| More
Red face Define string in C++

I want to display char string in C++.
How it can be displayed. What is the keyword for
displaying the char string in c++.
Reply With Quote
  #2  
Old 02-02-2012, 07:02 PM
hobbyist's Avatar
Member
 
Join Date: Jan 2012
Posts: 55
Thanks: 1
Thanked 20 Times in 16 Posts
hobbyist is on a distinguished road
| More
Re: Define string in C++

Do you mean a c-style string?

Code:
char s[] = "some string";
std::cout << s << std::endl;
Note: a cstring is terminated by '\0' so that it can be used in string functions like strcmp. you can have a char array as well.

Code:
char char_array[] = { 'h', 'e', 'l', 'l', 'o' };
// if you tried to output that, you may wind up with trailing junk characters.
std::cout << char_array << std::endl;
Code:
char char_array[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
std::cout << char_array << std::endl;
// good to go
HTH
Reply With Quote
  #3  
Old 03-21-2012, 04:19 AM
Member
 
Join Date: Feb 2011
Location: CA, USA
Posts: 35
Thanks: 0
Thanked 4 Times in 4 Posts
johnBMitchell is on a distinguished road
| More
Re: Define string in C++

Strings are the most important part in programming .Here I give you simple example of display string.

Suppose we use A, B, C, and D as a character
It is initialized as followed
Char E 1=’A’, E2=’B’, E3=’C’, E4=’D’;
For display this char in group(string)
Cout <<”the string is “<<E1<<E2<<E3<<E4;
Program for it
#include <iostream>
using namespace std;
int main()
{
Char E 1=’A’, E2=’B’, E3=’C’, E4=’D’;
Cout <<”the string is : “<<E1<<E2<<E3<<E4;
return 0;
}

Output of program
The string is: ABCD
Reply With Quote
  #4  
Old 03-21-2012, 02:14 PM
hobbyist's Avatar
Member
 
Join Date: Jan 2012
Posts: 55
Thanks: 1
Thanked 20 Times in 16 Posts
hobbyist is on a distinguished road
| More
Re: Define string in C++

That might confuse someone new to either C/C++.

That's not a string by definition; in fact, you couldn't use it with any standard library string functions. You should also avoid using a global namespace like std - later on, it may cause problems with larger programs.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
#define question SunnySmile MFC Programming 0 09-29-2010 02:30 PM
need to grep a string in a string kandarpa Shell Scripting 0 08-28-2010 03:11 AM
How could i define this? inspiration C Programming 2 04-15-2010 07:10 PM
How to define MSWIN ceal21c C Programming 0 01-28-2009 02:51 PM
How to insert a string into string? Tran C Programming 3 05-21-2008 09:51 PM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:42 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board