0% found this document useful (0 votes)
3 views9 pages

String For Coding

string methods

Uploaded by

Parmar Hiren
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

String For Coding

string methods

Uploaded by

Parmar Hiren
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/////////////// Reverse the string ////////////////////

String str=new String("hiren");


[Link](str);

String nstr="";
int n=[Link]();

for(int i=0;i<n;i++)
{
char temp=[Link](i);
nstr=temp+nstr;

[Link](nstr);

/*

//////////////// Longest common prefix/////////////////


/////////// here it will check the string lexicographically at a
time of sorting /////////////////////

*/
String stArr[]={"geeksForgeeks","geeks","geeksFor","geezer"};
[Link](stArr);
[Link]("after sorting the string array : ");

for(int i=0;i<[Link];i++){
[Link](stArr[i]+" ");
}
[Link](" ");

// int end=[Link](stArr[0].length(),stArr[[Link]-
1].length());
// [Link](end);

int len=stArr[0].length();
int count=0;

for(int i=0;i<len;i++){
if(stArr[0].charAt(i)==stArr[[Link]-1].charAt(i)){
count++;
}
}

String newStr= stArr[0].substring(0,count);


[Link]("Longest Common Prefix :"+newStr);

/*
/////////////// longest odd number in string ///////////////////

String s="9532432764";

int max=0;

for(int i=0;i<[Link]();i++){

if((int)[Link](i)%2==1){

if((int)[Link](i)>[Link](max)){
max=i;
}

[Link]("Maximum : "+[Link](max));

///////////// isomorphic string //////////////

String s="eggg";
String t="addd";

if([Link]()==[Link]()){

int mainFlag=0;

for(int i=0;i<[Link]();i++) {

int sCount = 0;
int tCount = 0;

for (int j = 1; j < [Link](); j++) {

if([Link](i)==[Link](j)){
sCount++;
}
if([Link](i)==[Link](j)){
tCount++;
}

}
if(sCount!=tCount){
[Link]("Not isomorphic!!!");
mainFlag=1;
return;
}

if(mainFlag==0){
[Link]("Isomorphic!!!");
}

}
else{
[Link]("Not isomorphic");
return;
}

/////////////// sub string /////////////////

String s="abc";
for(int i=0;i<[Link]();i++){

for(int j=i;j<=[Link]();j++){
[Link]([Link](i,j)+" ");
}
}

////////////////// Number of space /////////////

String s="abc aayz def dfd fgds";


int numberOfChar=1; ////// initialize with 1
/// if you specify with 0 then total number of character is , 1 less than
original string

for(char i='a';i<'z';i++){

for(int j=0;j<[Link]();j++){

if([Link](j)==i){
numberOfChar++;

}
}
}

int totalLen=[Link]();

int space=totalLen-numberOfChar;

[Link]("numberOfChar:"+numberOfChar);
[Link]("totalLen:"+totalLen);
[Link]("space: "+ space);

int word=space+1; // total number of word is always 1 more than the space

[Link]("Total Nuber of Word is : "+ word);

////////////// Print even length words in a string


////////////////

String s="abc xyzw bcf ab";

String arr[]=[Link](" ");


for (int i=0;i<[Link];i++){
[Link](arr[i]);
}

[Link]("even number of words in string : ");


for(int i=0;i<[Link];i++){
if(arr[i].length()%2==0){
[Link](arr[i]+" ");
}
}

//////////////// sum of two string //////////////////

String s="111";
String t="222";

int n=[Link](s)+[Link](t);

[Link](n);

[Link]([Link](n));
//////////// comparing two strings without in-built function
///////////////////

String s="abbc";
String t="abbc";

// ignoring the case of string

s=[Link]();
t=[Link]();

if([Link]()==[Link]()){

int flag=0;

for(int i=0;i<[Link]();i++){
if([Link](i)!=[Link](i)){
flag=1;
}
}

if(flag==0){
[Link]("both strings are same!!!");
}
else{
[Link]("strings are not same!!!");
}

else{
[Link]("strings are not same!!!");
}

//////////////// count number of vowels and consonants in string


///////////////

String s="abcdiweof";

int vowel=0;
int consonant=0;

for(int i=0;i<[Link]();i++){

if([Link](i)=='a'||[Link](i)=='e' ||[Link](i)=='i'
||[Link](i)=='o' ||[Link](i)=='u' ){
vowel++;
}

else{
consonant++;
}
}

[Link]("Number of vowels : "+vowel);


[Link]("Number of consonants : "+consonant);

int arr[]={3,3};
int target=6;
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
if(arr[i]+arr[j]==target){
[Link]("["+i+" , "+j+"]");
break;
}
}
}

// remove special character from the string


String s="a!bc@$de%f";
String ns="";
for(int i=0;i<[Link]();i++){
if([Link](i)>='a' && [Link](i)<='z' || [Link](i)>='A' &&
[Link](i)<='Z' ){
ns=ns+[Link](i);
}
}
[Link](ns);

// remove white space


String s="abc def";
String s1="";

for(int i=0;i<[Link]();i++){
if([Link](i)>='a' && [Link](i)<='z' || [Link](i)>='A' &&
[Link](i)<='Z' ){
s1=s1+[Link](i);
}
}
[Link](s1);
// remove duplicate element

String s="abscabdef";

String s1="";
for(int i=0;i<[Link]();i++){

int flag=0;
for(int j=i+1;j<[Link]();j++){

if([Link](i)==[Link](j)){
flag=1;
}
}
if(flag==0){
s1=s1+[Link](i);
}

[Link](s1);

// removing repeating element without changing the order

String s="abcdabgh";
String st="";

for(int i=0;i<[Link]();i++){
int flag=0;
for(int j=0;j<[Link]();j++){

if([Link](i)==[Link](j)){
flag=1;

int innerFlag=1;
for(int k=0;k<[Link]();k++){
if([Link](k)==[Link](i)){
innerFlag=0;
}
}
if(innerFlag==1){
st=st+[Link](i);
}

}
if(flag==0){
st=st+[Link](i);
}
}
[Link](st);
// sorting the array

String s="dcsfdfba";

char arr[]= new char[[Link]()];

arr=[Link]();

for(int i=0;i<[Link];i++){

for(int j=i+1;j<[Link];j++){
if(arr[i]>arr[j]){

char tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}

}
[Link](new String(arr));

// replace character T with it's occurences


String s="OPENTEXT";
String st="";

int count=1;
for(int i=0;i<[Link]();i++){

if([Link](i)=='T'){
st=st+count;
count++;
}
else{
st=st+[Link](i);
}

}
[Link](st);
String s="aabcdbe";

for(int i=0;i<[Link]();i++){
int flag=0;
for(int j=0;j<[Link]();j++){
if(i!=j && [Link](i)==[Link](j)){
flag=1;

}
if(flag==0){
[Link]("First Non repeating Element :"+[Link](i));
break;
}
}

You might also like