Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Find occurrences of each character in a string

Tue Apr 17, 2012 9:35 am

Find occurrences of each character in a string
Given two strings s1 and s2 as parameters to your function, find out the number of
occurrences of each character of s2 in s1 and return a string which has the count(in s1) of
each character of s2 in order that the characters occur in s2. (Note: The function should be
case sensitive.)
Sample Run Input:
s1 = "RenaissaNce"
s2="aRaans"
Output:
"a2R1n1s2"




Re: need answer to the following question

Sat May 12, 2012 10:25 pm

You will need to count the occurrences of a Character in a string ,
Code:

int FindOccurrences 
(String s,char mychar) {
  int count = 0;

  for (int i = 0; i < s.size(); i++)
    if (s[i] == mychar) count++;

  return count;
}
 


in java so
Code:
String string1="Value1";
        String string2="Value2";
        char x='a';
        int found=0;
        for(int i=0;i<string1.length();i++)
        {
            if(string1.charAt(i)==x){
                found++;
            }
       
        }


Post a reply
  Related Posts  to : Find occurrences of each character in a string
 Number of Occurrences of characters in a string     -  
 find a string     -  
 How to find server IP, PORT using String     -  
 Character Operations     -  
 character recognition     -  
 ANSI code value of character     -  
 Reading a Specific Character in php     -  
 Sending One lakh character in ajax     -  
 Handle Key event-keyboard-Get typed Character and its code     -  
 character running automatically / jump /duck/ functions     -  

Topic Tags

Java Strings