New course! Every coder should learn Generative AI!
Try a free lesson+ 13
Ali you can do like this way
You can get the length of the string by using strlen() function by making an counter for loop till the null terminator. You can take string by gets() function.
for (i=0; str[i]!='\0'; i++)
{
//Code blocks
}
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string str;
cin >> str;
cout << "Length of string : " << strlen(str.c_str());
return 0;
}
or if you're using char array
char s[]="hello";
cout<<sizeof(s)/sizeof(s[0]); //6, because it end with \0
You can use vector for pushing elements
std::vector< int > arr;
arr.push_back(10);
arr.push_back(12);
https://www.sololearn.com/learn/261/?ref=app
Or you can use list in C++ for completing that task.
std::list<int> arr;
for (int i = 0; i < 10; i++) {
arr.push_back(i);
}
Here are an good reference to that go through this
https://en.cppreference.com/w/cpp/container/list
0
You can also use "some text".size() or "some text".length()