Examples of strings in c++ programing

0

String is a collection of characters. There are two types of strings in C++ programming language:

Strings that are objects of string class (The Standard C++ Library string class)

C-strings (C-style Strin gs)

All Contributions

write a program to enter two strings and store them then show the entered strings:

#include <iostream>

using namespace std;

void display(char *);
void display(string);

int main()
{
    string str1;
    char str[100];

    cout << "Enter a string: ";
    getline(cin, str1);

    cout << "Enter another string: ";
    cin.get(str, 100, '\n');

    display(str1);
    display(str);
    return 0;
}

void display(char s[])
{
    cout << "Entered char array is: " << s << endl;
}
void display(string s)
{
    cout << "Entered string is: " << s << endl;
}

How to Write a program that enter a data type string:

#include <iostream>
using namespace std;

int main()
{
    string str;
    cout << "Enter a string: ";
    getline(cin, str);
    cout << "You entered: " << str << endl;
    return 0;
}

how to Write a program to read and display an entire line entered by user :

#include <iostream>
using namespace std;
int main()
{
    char str[100];
    cout << "Enter a string: ";
    cin.get(str, 100);
    cout << "You entered: " << str << endl;

    return 0;
}

How to Write a program to display a string entered by user.

#include <iostream>
using namespace std;
int main()
{
    char str[100];
    cout << "Enter a string: ";
    cin >> str;
    cout << "You entered: " << str << endl;
    cout << "\nEnter another string: ";
    cin >> str;
    cout << "You entered: "<<str<<endl;

    return 0;
}

a string entered by user. 

total contributions (4)

New to examplegeek.com?

Join us