Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:57 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: C++ Strings
PostPosted: Thu Nov 13, 2008 1:52 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

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.


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 remove space ,trim strings from left side ,right side ,both.
 reverse ,length of strings as number of bytes
 compare strings
 Help with selection sort for strings?
 compare two strings
 compare strings with if statement in php
 Compare two strings in php
 Trimming Strings in php
 PHP Strings
 Lets Learn C++----->(Lesson 3) Strings

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team