Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: String Data Type | Text Data Type
C++ Data Types

bookChallenge: String Data Type

Instead of working with individual characters, we usually work with complete words, sentences, or texts sequences of characters. One way to represent these sequences is with a char array, like this:

main.cpp

main.cpp

copy
12345678910
#include <iostream> int main() { // The '\0' is a null terminator, used to indicate the end of the string // It ensures that the array of characters is treated as a proper string char word[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; std::cout << word << std::endl; }

As you can see, initializing a character array can be tricky. Moreover, modifying the text, such as adding more characters, requires redefining the array with additional memory.

Thankfully, you can use the string class, which simplifies this process significantly.

string.h

string.h

copy
1
std::string text = "Any text in double quotes";

So you can assign to a string any text within double quotes " ". Also, adding more text to a string is as easy as using the .append() method. Here is an example:

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { // Declare and initialize a string std::string word = "Codefinity"; std::cout << word << std::endl; // Use the append() method to add ".com" to the string. word.append(".com"); std::cout << word << std::endl; }

Besides .append(), there are many other methods of a string to allow you efficiently operate with text data. Here is the table with some. They will be discussed in more detail in later chapters.

Uppgift

Swipe to start coding

You are building a system that formats full addresses for users. Your task is to combine the street, city, and country into a single formatted address.

The function createFullAddress takes three strings: street, city, and country.

  1. Inside createFullAddress, combine the strings in the format: "street, city, country".
  2. Use string methods to append ", " and the other parts of the address (append()).
  3. Return the resulting string as the full address.

Example

createFullAddress("123 Streeet", "New York", "USA") => "123 Streeet, New York, USA"
createFullAddress("456 Main St", "Los Angeles", "USA") => "456 Main St, Los Angeles, USA"

Lösning

solution.cpp

solution.cpp

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain how to use some of these string methods with examples?

What is the difference between .find() and .rfind()?

How does .capacity() differ from .length()?

close

Awesome!

Completion rate improved to 4.35

bookChallenge: String Data Type

Svep för att visa menyn

Instead of working with individual characters, we usually work with complete words, sentences, or texts sequences of characters. One way to represent these sequences is with a char array, like this:

main.cpp

main.cpp

copy
12345678910
#include <iostream> int main() { // The '\0' is a null terminator, used to indicate the end of the string // It ensures that the array of characters is treated as a proper string char word[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; std::cout << word << std::endl; }

As you can see, initializing a character array can be tricky. Moreover, modifying the text, such as adding more characters, requires redefining the array with additional memory.

Thankfully, you can use the string class, which simplifies this process significantly.

string.h

string.h

copy
1
std::string text = "Any text in double quotes";

So you can assign to a string any text within double quotes " ". Also, adding more text to a string is as easy as using the .append() method. Here is an example:

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { // Declare and initialize a string std::string word = "Codefinity"; std::cout << word << std::endl; // Use the append() method to add ".com" to the string. word.append(".com"); std::cout << word << std::endl; }

Besides .append(), there are many other methods of a string to allow you efficiently operate with text data. Here is the table with some. They will be discussed in more detail in later chapters.

Uppgift

Swipe to start coding

You are building a system that formats full addresses for users. Your task is to combine the street, city, and country into a single formatted address.

The function createFullAddress takes three strings: street, city, and country.

  1. Inside createFullAddress, combine the strings in the format: "street, city, country".
  2. Use string methods to append ", " and the other parts of the address (append()).
  3. Return the resulting string as the full address.

Example

createFullAddress("123 Streeet", "New York", "USA") => "123 Streeet, New York, USA"
createFullAddress("456 Main St", "Los Angeles", "USA") => "456 Main St, Los Angeles, USA"

Lösning

solution.cpp

solution.cpp

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
single

single

some-alt