Total members 11889 |It is currently Fri Mar 29, 2024 2:23 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





cpp code
#include <iostream>
#include <string>

using namespace std;

main()
{
// Get some parts of speech, and combine them.
cout << "Proper Noun? ";
string res;
cin >> res;

cout << "Transitive Verb? ";
string s;
cin >> s;
res += " " + s + " the ";

cout << "Noun? ";
cin >> s;
res += s;

cout << "Adverb? ";
cin >> s;
res += " " + s + ".";

// Check this one.
if(s.substr(s.length() - 2, 2) != "ly")
cout << "Hmmm. Adverbs _usually_ end in \"ly.\"" << endl;

cout << "Useless sentence: " << res << endl;

int pos = res.find("q q");
if(pos != string::npos)
cout << "Wow! Your sentence contains q, space, q "
<< "in position " << pos << ".\n"
<< " How likely is that?" << endl;


The C++ string facility is very good. Later in the term, we will talk about the rather quirky string facility of plain C, and discover that C++ loads better. The C++ string facility is reminiscent of Java's, but has many differences. Some are:

* In C++, you say string, not String.

* C++ strings are C++ objects, not Java-style references. Therefore, comparison using == and other relationals really works, and assignment with = makes a separate, independent copy, much like Java clone.

* In C++, strings not used so extensively for I/O purposes, mostly because streams is quite powerful.

One other unfamiliar thing in the program is the name string::npos. This is simply a named constant integer value declared in the standard library's string class. (It's probably -1, but I haven't tried to find out.) As we will see much later when we get to classes, C++ uses the :: notion to select members of a class, and reserves the dot for members of an object. Java uses the dot for both purposes.

And one final warning: in Java, double-quoted constants are strings. In C++, they are not. (As for what they are, and why, you must wait.) In most cases, conversion to string is automatic, but if you get mysterious errors, say string("whatever") to make an explicit conversion.



_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : C++ Strings
 Sort strings java-Sorting Array of Strings     -  
 PHP Strings     -  
 compare two strings     -  
 Use Switch with strings     -  
 compare strings     -  
 Trimming Strings in php     -  
 Compare two strings in php     -  
 Iterate on array of strings     -  
 compare strings with if statement in php     -  
 Help with selection sort for strings?     -  



Topic Tags

C++ Strings






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com