PDA

View Full Version : سوال: یافتن طول یک رشته



sanchez222
سه شنبه 28 دی 1389, 21:34 عصر
سلام
در زبان c چطور باید طول یک رشته رو پیدا کرد؟
ممنون.

r00tkit
سه شنبه 28 دی 1389, 21:40 عصر
char str[20] = "hello world";
cout << strlen(str) << endl;

std::string str1 = "hello world";
cout << str1.length() << endl;

Salar Ashgi
شنبه 02 بهمن 1389, 11:25 صبح
یک مثال تقریبا کاربردی :



#include <iostream>
#include <conio>
#include <string>
using namespace std;
int main(){
char a[]={"This is test string"};
int len=0;
for(;a[len]!=NULL;len++){}
cout<<len<<endl;
//---------------
cout<<strlen(a)<<endl;
//---------------
string str = "This is test string";
cout<<str.size()<<" "<<str.length()<<" "<<str.capacity()<<endl;
getch();
}


موفق باشید ./