Archive for the ‘string program’ Category

sorting any char array..danagram..

August 30, 2007

#include<iostream>
using namespace std;

main()
{
char a[30];
cout<<“\n enter the char string :”;
cin>>a;
int i=0,count=0;
while(a[i]!=”)
{ count++; i++; }
for(int j=0;j<count;j++)
{ for(i=0;i<count;i++)
if((int)a[i]>(int)a[i+1])
{ int temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
for(i=0;i<count;i++)
cout<<a[i];
cout<<endl<<endl;
system(“pause”);
}

Advertisements

return the given char position in any given string n last position

August 29, 2007

#include<iostream>
using namespace std;
int main ()
{
char str[80],ch;
cout<<“\n enter the string :”;
cin.get(str,80);
cout<<“\n enter the char:->”;
cin>>ch;
int i=0;
while(str[i]!=”)
{
if(str[i]==ch)
{ cout<<i<<” “; i++;
continue; }

i++;
}
cout<<endl<<endl;
system(“pause”);
return 0;
}

//few person ask to return the last position of that particular char in any given string…so just modify lil bit n don print cout<<i<<” “; see below..

//it won work if the given input started from that char like kkkk…. n char=k; //it’ll return 0

#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[80],ch,count[30];
cout<<“\n enter the string :”;
cin.get(str,80);
cout<<“\n enter the char:->”;
cin>>ch;
int i=0,j=0;
while(str[i]!=”)
{
if(str[i]==ch)
{ count[j]=i; j++;
i++;
continue; }
i++;
}
int c=strlen(count);
cout<<(int)count[c-1]; //(int)for showing the value otherwise it’ll show address cout<<endl<<endl;
system(“pause”);
return 0;
}

//another method beter than previous 

#include<iostream>
using namespace std;
int main ()
{
char str[30],ch;
cout<<“\n enter the string :”;
cin.get(str,30);
cout<<“\n enter the char:->”;
cin>>ch;
int i=0,z=0;
while(str[i]!=”)
{
if(str[i]==ch)
{   z=i;
i++; continue; }
i++;
}
cout<<z;    //it’ll print tha last index of that char in given  string
cout<<endl<<endl;
system(“pause”);
return 0;
}

Covert a string to upper case

August 28, 2007

void ToUpper(char * S)
{
while (*S!=0)
{
*S=(*S >= ‘a’ && *S <= ‘z’)?(*S-‘a’+‘A’):*S;
S++;
}
}

//dancing doll…..means lower->upper n vice-versa…its simple to implement

//A-Z(65-90) n a-z(97-122) ASCII value..

#include<iostream>
using namespace std;

main()
{
char b;
cout<<“\n enter any char :”;
while(cin>>b)
{
if(int(b)>96)
cout<<” its inverted is :”<<(char)((int)b-32)<<“\n\n enter any char: “;
else if(int(b)<97)
cout<<” its inverted is :”<<(char)((int)b+32)<<“\n\n enter any char: “;
}

cout<<endl<<endl;
system(“pause”);
}

reverse the string

August 27, 2007

//1st method

#include<iostream>
using namespace std;

void reverse(char a[],int start,int end)
{
char temp=a[start];
a[start]=a[end];
a[end]=temp;

start++;
end–;
if(start<end)
reverse(a,start,end);
}

main()
{
char a[50];
int i=0,count=0;
cout<<“\n enter the string to be reversed :”;
cin.get(a,50);
while(a[i]!=0)
{
count++;
i++;
}
i=0;
reverse(a,0,count-1);
cout<<a;

cout<<endl<<endl;
system(“pause”);

}

//2nd method

#include<iostream>

using namespace std;
main()
{
char a[300];
int i=0,count=0;
cout<<“\n enter the string :”;
cin.get(a,300);
cout<<“\n ur string is :”<<a;
while(a[i]!=”)
{
count++;
i++;
}
cout<<“\n length of the string is :”<<count<<endl;
cout<<“\n reverse of sentense is :”;
for(int j=count-1;j>=0;j–)
{ cout<<a[j]; }

cout<<endl<<endl<<endl;
system(“pause”);
}

//3rd method 

void ReverseString (char *String)

{

char *Begin = String;

char *End = String + strlen(String)1;

char TempChar;

while (Begin < End)
{
TempChar = *Begin;
*Begin = *End;
*End = TempChar;
Begin++;
End- -;
}
}