Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Saturday, March 18, 2017

C++ Part 31

String.Length

//Demonstrates using a string object as if it were an array.
#include <iostream>
#include <string>
using namespace std;
int main( )
{
   string firstName, lastName;
   cout << "Enter your first and last name:\n";
   cin >> firstName >> lastName;
   cout << "Your last name is spelled:\n";
   int i;
   for (i = 0; i < lastName.length( ); i++)
   {
       cout << lastName[i] << " "; // Printing the last name with a space after that.
       lastName[i] = '-'; // Replacing the LastName with a "-" character. You not see the result yet.
   }
   cout << endl;
   for (i = 0; i < lastName.length( ); i++)
       cout << lastName[i] << " "; //Places a "-" under each letter, and prints a space after theme.
   cout << endl;
   cout << "Good day " << firstName <<"."<< endl;
   return 0;
}

Friday, March 17, 2017

C++ Part 30

String | Getline and Cin


string s1, s2;
cin >> s1;
cin >> s2;
If the user types in
I love NavidPlus website.
s1 will receive value "I" and s2 will receive the value "love".
If you want your program to read an entire line of input into a variable of type string , you can use the function getline.
Example:
string line;
cout << "Enter a line of input:\n"; getline(cin, line); cout << line << "END OF OUTPUT\n";

I/O with string Objects
You can use the insertion operator << with cout to output string objects. You can input a string with the extraction operator >> and cin.
  • When using >> for input, the code reads in a string delimited with whitespace.
    You can use the function getline to input an entire line of text into a string object.
    EXAMPLES
    string greeting("Hello"), response, nextLine;
    cout << greeting; cin >> response;
    getline(cin, nextLine);
  • Thursday, March 16, 2017

    C++ Part 29

    C++ Strings Part II

    If you didn't read the first part, you can find it here.
    getline: may input spaces, as before.
    e.g., getline(cin,s1) would assign “Navid Plus” to s1 in the "Navid Plus" example.
    Example :

    #include <iostream>// Coded by https://NavidPlus.blogspot.com#include <string>
    #include<cstdlib>
    using namespace std;

    int main ( )
    {
        string backString;
        cout << "What ever your going to write here, I will reverse it. " <<endl;
        getline (cin,backString);
        int theIndex = backString.length()-1;
        while ( theIndex >= 0)
        {
           cout << backString [theIndex] ;
           theIndex --;
        }
        cout << '\n' ;
    }

    C++ Part 28

    Strings


    A char literal is indicated with single quotes (e.g., ’c’), while a string literal is indicated with double quotes (e.g., “abc”).
    These are C++ strings and are in library:
    • =: Assignment
    • <,> , <=, >= ,== , ! =, . . .:
    • s[i]: Indexing, i.e., i’th character of s (i ≥ 0)
      •  Ex: string s = ‘‘cat’’; char c = s[2] sets c to ’t’
    •  + :  Concatenation (overloaded for characters)
      • Ex: ‘‘apple’’ + ‘‘pear’’ evaluates to “applepear” while ‘‘apple’’+’s’ evaluates to “apples”
    • s.empty(): true if s is an empty string 
    • s.length(): length of s (≥ 0) 
    //Assignment and comparison operators work for strings but NOT
    for all C++ data types.

    <<, >> : input/output (still uses whitespace as delimiter).
    e.g., cin >> s1 >> s2 would assign “Navid” to s1 and “Plus” to s2,
    if the user typed “Navid Plus”.

    Wednesday, March 15, 2017

    C++ Part 25

    Functions

    • Breaks code down into manageable chunks to write (and divide across development team)
    • Easier to debug – only one idea behind each module
    • Easier to maintain – buggy modules may be replaced without worrying about ramifications on other modules
    • Outsiders only need to know a module’s interface, not the details of its implementation.
    • Avoids duplication of code ⇒ more compact, and also easier to modify if code has bug  
    • Predefined functions: e.g., String.length(): String → Int  
    • Programmer-Defined functions: used similarly to predefined functions  

      This predefined functions need using namespace std;

    Source : Absolute C++



    C++ Part 24

    Working with String - File - While Loop


    //NavidPlus.blogspot.com
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main( )
    {
       string text;
      fstream inputStream;
       inputStream.open("Website");

       while (inputStream >> text)
    {
    cout << text << endl;
    }

       inputStream.close();
       return 0;
    }
    // This Program will read all the input while the condition still true.

    Input File : Webiste


    NavidPlus https://NavidPlus.blogspot.com
    NavidPlus@Blogger
    Navid Plus is a Tech blog which is focusing on a Science material that I'll be learned every day through my College and job. Most likely blog's posts contain Programming, Security, Networking, Math, and photography. It is a FUN blog.
    C Plus Plus @ NavidPlus
    INTRO
    Intro to C++ Part 1
    Intro to C++ Part 2
    Intro to C++ Part 3
    Intro to C++ Part 4
    Intro to C++ Part 5
    Intro to C++ Part 6
    Intro to C++ Part 7
    Intro to C++ Part 8
    _________________________________ C++ By NavidPlus.BlogSpot.Com

    If you run this Program, you will find the problem !?

    C++ Part 23

    Using Getline

    Reading From a Text File Using ifstream:

    To read from a text file we need to include the fstream library and create an ifstream object which is placed in the std namespace.


    #include <fstream>
    using namespace std;  


    Example


    //NavidPlus.blogspot.com
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main( )
    {
       string Name, Website;
       string description;
       fstream inputStream;
       inputStream.open("Website");

       inputStream >> Name >> Website;
       inputStream >> description;
       cout << "Name: " << Name << "      "  << "Website: " << Website << endl;
       cout << "description: " << description << endl;
       inputStream.close();
       return 0;
    }

    The input file is Website and must be in the same folder


    NavidPlus https://NavidPlus.blogspot.com
    NavidPlus@Blogger


    Digital Design Part 3

    4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...