C++ : Function with String in hindi , what is function with string program example in c++ language

what is function with string program example in c++ language , C++ : Function with String in hindi :-

इससे पहले के article मे function को array और struture के merge को discuss किया है अब इस article मे function को string के साथ use करने के लिए discuss करेगे |

String

string character का समूह होता है | string को use करने के लिए string class और chracter array को use किया जाता है | इसके लिए निन्म syntax होता है :

char a[];

ये syntax से character array को use किया जाता है |  इसमें character array को use करने के लिए cout और cin statement को use किया जाता है  | string class के function को use करने के लिए string.h header file को use किया जाता है | इसके लिए निन्म syntax को use किया जाता है |

string string_name ;

इस syntax मे data size को declare नहीं किया जाता है | string.cout() function और string.cin() function को string को read और write करने के लिए use किया जाता  है |

इसका उदाहरन निन्म है :-

#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

{

char first_name[20];

string last_name ;

cout<<“Enter First Name :”<<endl;

cin>>first_name;

cout<<“Enter Last Name :”<<endl;

string.cin(last_name);

cout<<“Name :”<<first_name last_name<<endl;

getch();

}

इस उदहारण मे ,first_name एक character array होता है | इसे read करने के लिए cout statement को use किया जाता है |  और last_name एक string variable है और string.cout() function को string variable को read करने के लिए use किया जाता है |

इसका आउटपुट होता है :-

Enter First Name : Parth

Enter Last Name : Patel

Name : Parth Patel

Function with string

जब किसी string को function मे pass किया जाता है  तब  string को function मे pass करने के लिए तीन method को use किया ह्जता है |

1.charaecter array को pass किया जाता है |

2.string lateral ये single quote मे क्लोज होता है इसे function मे pass किया जाता है |

3.pointer to char set को use किया जाता है इसमें string के address को use किया जाता है |

#include<iostream.h>

#include<conio.h>

int c_in(const char*s ,char c);

void main()

{

using namespace std;

char m[15]=”minimum”;

char *wait = “ululate”;

int_ms=c_in(m , ‘m’);

int_ms=c_in(wait, ‘u’);

cout<<ms<<“m character in “<<m<<endl;

cout<<ms<<“u character in “<<wail<<endl;

getch();

}

int c_in(const char*s ,char c)

{

int count = 0;

while(*s)

{

if(*s==c)

{

count++;

s++;

}

return count ;

}

इस उदाहरन मे  const keyword को use किया जाता है | const keyword को string variable को declare किया जाता है | string variable की जगह chracter array को use किया जाता है | syntax निन्म है :

int c_in(const char [] ,char c);

 इस उदहारण मे string pointer को भी use किया गया है | loop मे string variable ‘s’ को string variable ‘s’ मे assign किया गया है | और pointer variable को use किया जाता है |

इसका आउटपुट होगा :

3 m character in minimum

2 u character in ululate

जब किसी struture को function मे pass किया जाता है  तब struture को function मे तेनो methods मे से एक method को  use किया जाता है | और function मे struture को भी नार्मल variable की तरह return किया जाता है | उदाहरन के लिए :

#include<iostream.h>

#include<conio.h>

int stringlength ( string *s);

void main()

{

string name ;

cout<<“Enter Name :”;

cin>>name;

int length = stringlength ( &name ) ;

cout<<“Length :”<<length;

getch();

}

int stringlength ( string *s)

{

int count =o;

while(*s==’/0′)

{

count++;

}

return count ;

}

इस उदाहरन मे ,stringlength() function को use किया जाता है | function stringlength() से string की length को calculate किया जाता है | string variable को  stringlength () पास्ड किया जाता है | और इस variable को function definition मे एक और variable ‘s’ मे initail किया जाता है | और इसके बाद input() का use यूजर द्वारा input को लिया गया है |

इसका आउटपुट होगा :

Enter Name : Parth

Length : 5

इन उदाहरन के अलावा function मे string के दुसरे उदाहरन को consider करेगे : जिसमे string को function मे  pass किया जाता है | इन string मे operation को perform किया जाता ही | और बाद मे इस function को variable मे assign किया जाता है | और variable को display() मे pass किया जाता है |

#include<iostream.h>

#include<conio.h>

int input( string *s );

void display( string *o );

void main()

{

string  v;

v=input( string *s);

display(v);

getch();

}

void input( string *s )

{

cout<<“Enter Name : “;

string.cin(*s);

return s;

}

void display ( string *o )

{

cout<<“Name :”<<*o;

}

इस उदाहरन मे ,display()  और  input() function को use किया जाता है | function display() से string की value को display किया जाता है | string  variable को  display () पास्ड किया जाता है | और इस variable को function definition मे एक और variable ‘o’ मे initail किया जाता है | और इसके बाद input() का use यूजर द्वारा input को लिया गया है |

इसका आउटपुट होगा :

Enter Name : Parth

Name : Parth

इस article मे string को function मे pass और return करने के method को discuss किया जाता है | अब आगे के article मे class और object को discuss करेगे |