Tag: String
Concatinate two string without using string.h
by Binod on Apr.03, 2011, under C & C++
To Concatinate two string we first find out the total sum of two individual string and add them, then we copy the first string to temp variable[string] then we start to append the next string to temp variable from the length of first string till the total length.
Source Code:
#include<iostream.h>
#include<conio.h>
class binod{
int i,j,counta,countb,totl;
char a[10],b[10],newstr[20];
public:
void getdata();
void countlen();
void addstr();
void clrstr();
void showdata();
};
void binod::getdata(){
cout<<”Enter first string: “;
cin>>a;
cout<<”Enter second string: “;
cin>>b;
}
void binod::countlen(){
counta=0;
for(i=0;a[i]!=’\0′;i++){
newstr[i]=a[i];
counta++;
}
countb=0;
for(i=0;b[i]!=’\0′;i++){
countb++;
}
}
void binod::addstr(){
totl=0,j=0;
totl=counta+countb;
for(i=counta;i<totl;i++){
newstr[i]=b[j];
j++;
}
}
void binod::clrstr(){
for(i=0;i<20;i++){newstr[i]=’ ‘;}
}
void binod::showdata(){
cout<<endl<<”The Concatinated string is: “<<newstr;
}
main()
{
clrscr();
binod k;
k.clrstr();
k.getdata();
k.countlen();
k.addstr();
k.showdata();
getch();
return 0;
}
Find the length of a given string without using string.h.
by Binod on Apr.03, 2011, under C & C++
We know that each string has ‘\0’ at the end of it in the memory array. So to find the length, we read individual character of the string until it encounters ‘\0’.
Source Code:
#include<iostream.h>
#include<conio.h>
class binod{
int i,count;
char sen[10];
public:
void getdata(){
cout<<”Enter the string: “;
cin>>sen;
}
void showlen();
};
void binod::showlen(){ //For Counting length
count=0;
for(i=0;sen[i]!=’\0′;i++){
count++;
}
cout<<”The length of “<<sen<<” is: “<<count;
}
main(){
clrscr();
binod k;
k.getdata();
k.showlen();
getch();
return 0;
}