Ask Your Question
4

Is it possible to create an array of strings in C++ that have different sizes or share similar functionality?

asked 2023-02-03 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-11-27 10:00:00 +0000

ladyg gravatar image

Yes, it is possible to create an array of strings in C++ that have different sizes or share similar functionality. Here's an example:

#include <iostream>
#include <string>

int main() {
  std::string strings[] = {"hello", "world", "!", "C++ is", "fun"};

  // Printing the length of each string in the array
  for (auto str : strings) {
    std::cout << str << " has length " << str.length() << std::endl;
  }

  return 0;
}

In this example, we have created an array of strings with different lengths and functionality (e.g., some strings are punctuation marks while others are complete sentences). We can access each string in the array using its index (e.g., strings[0] would give us the string "hello"). We can also use string member functions (e.g., length()) on each element of the array to perform operations specific to that string.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-03 11:00:00 +0000

Seen: 8 times

Last updated: Nov 27 '21