Program to count the no. of times a character occurs in a string.

program to count the no. of times a character occurs in a string.
Say if u have a method :- Countclass(string s,char c) then using this u should be able to count a char ' T' in the string ="TESTING".
So it is appering 2 times.
How to count it.

int countChar(String s, char c) {
   int count = 0;
   for (int i=0; i<s.length(); i++) {
      if (s.charAt(i) == c)
         count++;
   return count;
}

Similar Messages

  • Count the number of times a character is in a string using pl/sql

    I need to count the number of times ":" appers in the string ":XXX:CCC:BBB:".
    I have sound some solution using SQL but I do not want the context switch.
    Also I am on 10g so I can not use REGEXP_COUNT.
    Any help would be great.

    Hi,
    length(REGEXP_REPLACE(':XXX:CCC:BBB:','[[:alnum:]]'))counts all kinds of punctuation, spaces, etc., not just colons. Change any (or all) of the colons to periods and it will still return 4. Use '[^:]' instead of '[[:alnum:]]' if you really want to count just colons.
    Also, "SELECT ... FROM dual" is usually needed only in SQL*Plus or similar front end tools. In PL/SQL, you can call functions without a query, like this:
    x := NVL (LENGTH (REGEXP_REPLACE (txt, '[^:]')), 0);

  • Need code to count the no.of time each character occurs in a sentence

    hi all i need code to find the count of each character in a sentecne

    1.) Don't ask questions that have been asked (and answered) a thousand times before:
    1a.) Google
    1b.) Search the forums
    2.) Show us what you already tried. You're getting help here, not your complete homework
    3.) How To Ask Questions The Smart Way

  • Find the Number of times a '¯'character appears in a string.

    Hi,
    In my staging table I am having data like below
    ABL¯ABL¯0¯0¯ABL¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ 
    ABL¯ABQ¯480¯825¯DLS¯AMA¯ABQ¯ ¯ ¯ ¯ ¯ ¯ ¯ 
    ABL¯ACD¯808¯1255¯DLS¯ELP¯TCS¯PHX¯ACD¯ ¯ ¯ ¯ ¯ 
    ABL¯ADE¯1256¯471¯DLS¯AMA¯ABQ¯LSV¯ADE¯ ¯ ¯ ¯ ¯ 
    ABL¯AFT¯1140¯1744¯DLS¯LAX¯FON¯AFT¯ ¯ ¯ ¯ ¯ ¯ 
    ABL¯AHM¯1178¯1637¯DLS¯LAX¯AHM¯ ¯ ¯ ¯ ¯ ¯ ¯ 
    ABL¯ALB¯1769¯1825¯DLS¯WIL¯ALB¯ ¯ ¯ ¯ ¯ ¯ ¯ 
    ABL¯ALE¯1041¯1150¯DLS¯ALE¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯
    Now I want to find the Number of times a '¯'character appears in a string.
    I should get output 14
    thanks & regards,
    Vipin Jha
    Thankx &amp; regards, Vipin jha MCP

    Hi Vipin,
    You can try below sql code also
    code:-  len(data)-len(replace(data,'¯','')) -(len(data)-len(replace(data,' ','')))
    select data,len(data)-len(replace(data,'¯','')) -(len(data)-len(replace(data,' ',''))) CountofCharacter from
    select 'ABL¯ABL¯0¯0¯ABL¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ' data
    UNION
    SELECT 'ABL¯ABQ¯480¯825¯DLS¯AMA¯ABQ¯ ¯ ¯ ¯ ¯ ¯ ¯ '
    UNION
    SELECT 'ABL¯ACD¯808¯1255¯DLS¯ELP¯TCS¯PHX¯ACD¯ ¯ ¯ ¯ ¯ '
    UNION
    SELECT 'ABL¯ADE¯1256¯471¯DLS¯AMA¯ABQ¯LSV¯ADE¯ ¯ ¯ ¯ ¯ '
    UNION
    SELECT 'ABL¯AFT¯1140¯1744¯DLS¯LAX¯FON¯AFT¯ ¯ ¯ ¯ ¯ ¯ '
    UNION
    SELECT 'ABL¯AHM¯1178¯1637¯DLS¯LAX¯AHM¯ ¯ ¯ ¯ ¯ ¯ ¯ '
    UNION
    SELECT 'ABL¯ALB¯1769¯1825¯DLS¯WIL¯ALB¯ ¯ ¯ ¯ ¯ ¯ ¯ '
    UNION
    SELECT 'ABL¯ALE¯1041¯1150¯DLS¯ALE¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯'
    ) t
    Thanks
    Prasad

  • Calculate the number of times a character appears in a string

    Please help.
    Is there a way to count a number of occurences of a particular character in a string? e.g. If you have 'DATABASE' as a string, can you calculate the number of times 'A' occurs in it? The answer is supposed to be 3, but can you calculate it in Oracle?
    Thanks in advance.

    Re: Any built-ins for occurance

  • Return amount of times a character is in a string?

    Hello, quick question. As the title says, how can i return the amount of times a character is in a string? character such as 'a', 'b', etc. Thank you all, appreciate it!

    thank you for your help! so would this be correct? (s is the string)
              int amount = 0;
              for(int x = 0; x <= s.length(); x++)
                   if (s.charAt(x) == value){
                        amount++;
              return amount;

  • Is there a way to count the number of times an array moves from positive to negative?

    I have an array of values, and I need to find the number of times that the array changes signs (from positive to negative, or vice versa). In other words from a graphical standpoint, how many times a certain line crosses the x-axis. Counting the number of times the array equals zero does not help however, because the array does not always equal exactly zero when it crosses the axis (ie, the points could move from .1 to -.1).
    Thanks for you help. Feel free to email me at [email protected] I only have lv 5.1.1 so if you attach any files, they cannot be version 6.0.

    Attached is a VI showing the # of Pos and Neg numbers in an array, with 0 considered as non-Pos. It is easily modifiable to other parameters - including using the X-axis value as your compare point versus only Zero.
    This is a modified VI from LV (Separate Array.vi)
    Compare this with your other responses to find the best fit.
    Doug
    Attachments:
    arraysizesposneg.vi ‏40 KB

  • Java Program+To count the number of lines in a method excluding comments???

    Hi friends
    can u plz help me out, the java program is counting the number of lines in a method excluding comments.
    The first thing is how to identify a method, then there can be an inner method inside the parent method,
    Please friends its urgent
    Bye
    Sandy

    There's no such thing as an inner method in Java. You can either write the code yourself to parse Java source, or maybe something like ANTLR can do it.

  • I need to WAP to count the number of occurences of an alphabet in a string.

    I need to WAP to count the number of occurences of an alphabet in a string.I tried a lot and have surfed a lot regarding this problem.
    I m not the most proficient with java.but this is all i could come up with,and would appreciate some help here.I hope you guys would help me find a solution to this.
    e.g String : abcabrty
    Result should be
    a:2
    b:2
    c:1
    r:1
    t:1
    y:1
    public class chkoccurences
         public static void main(String args[ ])
              String user_Data=args[0];
              int counter=0;     
              try
                   for(int i=0;i<user_Data.length( );i++)
                        for(int j=0;j<user_Data.length( );j++)
                             if(user_Data.charAt(i) == user_Data.charAt(j))
                             counter++;
                        System.out.println(user_Data.charAt(i)+" exists "+counter+" time(s) in the word.");
                   System.out.println(" ");
              catch(ArrayIndexOutOfBoundsException e)
                   System.out.println("Check the array size.");
    }This is the output i get out of the program:
    a exists 2 time(s) in the word.
    b exists 4 time(s) in the word.
    c exists 5 time(s) in the word.
    a exists 7 time(s) in the word.
    b exists 9 time(s) in the word.
    r exists 10 time(s) in the word.
    t exists 11 time(s) in the word.
    y exists 12 time(s) in the word.What i think is i need an array to store the repeated characters because the repeated characters are getting counted again in the loop,if you know what i mean.
    Please, i would appreciate some help here.

    Criticism is welcomed
    public class tests {
         final int min = 10;
         final int max = 35;
         final char[] chars = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                   'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                   'v', 'w', 'x', 'y', 'z'};
         public static void main(String[] args){
              tests t = new tests();
              String[] strings = new String[] {"aabcze", "att3%a", ""};
              for(String s : strings){
                   System.out.println(t.getAlphaCount(s));
         public String getAlphaCount(String s){
              int[] alphaCount = new int[26];
              int val;
              for(char c : s.toCharArray()){
                   val = Character.getNumericValue(c);
                   if( (val>=min) && (val<=max)){
                        alphaCount[val-min]++;
              StringBuilder result = new StringBuilder();
              for(int i=0; i<alphaCount.length; i++){
                   if(alphaCount[i] > 0){
                        result.append(chars[i] + ":" + alphaCount[i] + ", ");
              if(result.length() != 0){
                   result.delete(result.length()-2, result.length());
              return result.toString();
    }

  • How do i count the number of times a cell has been referenced by other cells' formulae?

    Hello,
    I have a spreadsheet of recipes and ingredients that use checkboxes to let me know which recipes I can make based on the ingredients I have on-hand, using a formula like this one: =IF(AND('Spirits' :: A42,Mixers :: A18,Mixers :: A41,'Spirits' :: A25 ),Table 3 :: $A$2,Table 3 :: $A$3). If I've checked all of the boxes for ingredients that are required for any one of my recipes, the cell that the formula is in returns something like "YES"; otherwise, it returns a blank. It works well enough, but I'd like to include a cell next to each ingredient with a formula that will look through the hundreds of recipes, which all use a formula like the one above, and return a count of the number of times the ingredient's cell is referenced in those formulas. That way, I'll know which ingredients are the most commonly used in my eclectic collection of recipes. I thought something like =COUNTIF('Recipes' :: A2:A343, "=A2") would work, but it doesn't look at the cell references in the formulas, so no, it doesn't work. Any ideas? I'm using Numbers 2.3

    Hello
    There's no formula to inspect other formula in Numbers. So what you asked cannot be achived in such a way you described.
    A simple alternative method would be to build recipe-ingredient matrix as follows.
    E.g.
    Table definitions:
    Recipes
    A1    Recipe \ Ingredient
    A2    A
    A3    B
    A4    C
    A5    D
    A6    E
    A7    F
    A8    # recipes / ingredient
    B1    X1
    B2    TRUE
    B3    FALSE
    B4    TRUE
    B5    FALSE
    B6    TRUE
    B7    FALSE
    B8    =COUNTIF(B,TRUE)
    C1    X2
    C2    TRUE
    C3    TRUE
    C4    FALSE
    C5    TRUE
    C6    TRUE
    C7    TRUE
    C8    =COUNTIF(C,TRUE)
    D1    X3
    D2    FALSE
    D3    TRUE
    D4    FALSE
    D5    TRUE
    D6    FALSE
    D7    TRUE
    D8    =COUNTIF(D,TRUE)
    E1    X4
    E2    FALSE
    E3    FALSE
    E4    FALSE
    E5    FALSE
    E6    FALSE
    E7    TRUE
    E8    =COUNTIF(E,TRUE)
    F1    Y1
    F2    TRUE
    F3    FALSE
    F4    TRUE
    F5    TRUE
    F6    TRUE
    F7    FALSE
    F8    =COUNTIF(F,TRUE)
    G1    Y2
    G2    FALSE
    G3    TRUE
    G4    TRUE
    G5    FALSE
    G6    FALSE
    G7    TRUE
    G8    =COUNTIF(G,TRUE)
    H1    Y3
    H2    TRUE
    H3    TRUE
    H4    FALSE
    H5    TRUE
    H6    TRUE
    H7    FALSE
    H8    =COUNTIF(H,TRUE)
    I1    Y4
    I2    FALSE
    I3    FALSE
    I4    FALSE
    I5    FALSE
    I6    FALSE
    I7    TRUE
    I8    =COUNTIF(I,TRUE)
    J1    in-stock?
    J2    =SUMIF(B2:E2,TRUE,TRANSPOSE(X::$C$2:$C$5))+SUMIF(F2:I2,TRUE,TRANSPOSE(Y::$C$2:$C$5))=COUNTIF(B2:I2,TRUE)
    J3    =SUMIF(B3:E3,TRUE,TRANSPOSE(X::$C$2:$C$5))+SUMIF(F3:I3,TRUE,TRANSPOSE(Y::$C$2:$C$5))=COUNTIF(B3:I3,TRUE)
    J4    =SUMIF(B4:E4,TRUE,TRANSPOSE(X::$C$2:$C$5))+SUMIF(F4:I4,TRUE,TRANSPOSE(Y::$C$2:$C$5))=COUNTIF(B4:I4,TRUE)
    J5    =SUMIF(B5:E5,TRUE,TRANSPOSE(X::$C$2:$C$5))+SUMIF(F5:I5,TRUE,TRANSPOSE(Y::$C$2:$C$5))=COUNTIF(B5:I5,TRUE)
    J6    =SUMIF(B6:E6,TRUE,TRANSPOSE(X::$C$2:$C$5))+SUMIF(F6:I6,TRUE,TRANSPOSE(Y::$C$2:$C$5))=COUNTIF(B6:I6,TRUE)
    J7    =SUMIF(B7:E7,TRUE,TRANSPOSE(X::$C$2:$C$5))+SUMIF(F7:I7,TRUE,TRANSPOSE(Y::$C$2:$C$5))=COUNTIF(B7:I7,TRUE)
    J8   
    X
    A1    Ingredient
    A2    X1
    A3    X2
    A4    X3
    A5    X4
    B1    in-stock?
    B2    TRUE
    B3    TRUE
    B4    TRUE
    B5    FALSE
    C1    code
    C2    =IF(B2,1,0)
    C3    =IF(B3,1,0)
    C4    =IF(B4,1,0)
    C5    =IF(B5,1,0)
    Y
    A1    Ingredient
    A2    Y1
    A3    Y2
    A4    Y3
    A5    Y4
    B1    in-stock?
    B2    TRUE
    B3    FALSE
    B4    TRUE
    B5    FALSE
    C1    code
    C2    =IF(B2,1,0)
    C3    =IF(B3,1,0)
    C4    =IF(B4,1,0)
    C5    =IF(B5,1,0)
    In "Recipe" table, the first row is header row and the last row is footer row.
    Hope this may help,
    H

  • Need to count the number of times the Basic Finish data chages

    HI Expertes,
    I have a requirement I need to count number of times the Basic finish date chaged for PM work order. I went throug our forums I got some info like using a standard function module
    CHANGEDOCUMENT_READ_HDRS_ONLY
    CHANGEDOCUMENT_READ_HEADERS
    CHANGEDOCUMENT_READ_POSITIONS
    But all the above function module will not be suitable for my requirement since  CHANGEDOCUMENT_READ_HDRS_ONLY it gives whole changes but my requirement is just need number of changes occurred in Basic Finish date but CHANGEDOCUMENT_READ_POSITIONS can give the filed number which has been changed but still I need change id.
    So kindly suggest me wether there is any other Standard FM to get number of changes occurred in Basic Finish date?
    Thanks,
    Rajesh

    Hi Debbie,
         To count the number of groups please try the folling steps:
    1) Create a formula @reset and place this formula in the page header
        whileprintingrecords;
        numbervar i:=0;
    2) Create another formula @evalgroup place this in the group header where you want to count the values.
        whileprintingrecords;
        numbervar i:= i+1;
    3) Create another formula @display and place this in report footer.
        whileprintingrecords;
        numbervar i;
    In order to display the count of details which are printing in the detail section place the eval formula in the detail section and the @display formula in the group footer.
    Hope this helps!!!
    Regards,
    Vinay

  • How do I count the number of times a word appears in a column?

    What I have is a spreadsheet logging work history. Let's say the work location is "office", "home", or "travel" and I want to have a separate cell in another table for totals, count up the number of time each appears in the data for the year. So it would look like this:
    Date
    Location
    sept 1
    office
    sept 2
    home
    sept 3
    home
    sept 4
    travel
    sept 5
    office
    sept 6
    office
    sept 7
    office
    sept 8
    travel
    sept 9
    home
    sept 10
    office
    Totals
    Days
    10
    Office
    5
    Home
    3
    Travel
    2
    I just can't figure out how to develop the formula to put in the Totals column to make it work this way. If you can help me achieve this I'd really appreciate it!

    Hi SpartanAntarctican,
    Table 1 to record your locations
    Date
    Location
    sept 1
    office
    sept 2
    home
    sept 3
    home
    sept 4
    travel
    sept 5
    office
    sept 6
    office
    sept 7
    office
    sept 8
    travel
    sept 9
    home
    sept 10
    office
    Table 2 to sum your locations
    Totals
    Days
    10
    Office
    5
    Home
    3
    Travel
    2
    In Table 2 the formula in B2
    =ROWS(Table 1::A)−1
    Minus 1 because there is a Header Row in Table 1.
    In Table 2 the formula in B3 (and Fill Down)
    =COUNTIF(Table 1::B,A3)
    You can check this by adding 5 + 3 + 2 to arrive at 10
    Or maybe delete Row 2 put the Days calculation into a Footer Row in Table 2
    =SUM(B) .
    Regards,
    Ian.

  • BW Reporting - How to Count the number of times a query is executed

    Hi All,
    How do we get a report that enable us to get a count of number of times a report is executed? Example below:
    Info-provider         Report Name              Frequency
    PA_C01              Headcount                  24 times
    Any idea?
    Many thanks all.
    Mark Ng.

    Hi,
    1. Use Tcode = ST03N (Work Load Monitor)
    2. Choose Expert mode.
    3. Click on your system host name
    4. Click on Transaction Profile, then you can find out the all tasks.
    5. Select which ever you want to see details for queries.
    Regards,
    Srini Nookala

  • How can I count the number of times a timed loop finished late?

    I am getting occasional timing errors in a timed loop under windows XP and would like to count the number of errors, but I can't find a simple software counter vi

    The timed loop has a terminal (boolean) to indicate if the previous iteration finished late. Just increment an integer shift register if this happens.
    The attached shows a very simple demo (LabVIEW 7.1).
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    LateCounter.vi ‏76 KB

  • How do you count the amount of times a button is clicked?

    Heres my code:
    public void buttonClicked(JButton buttonzilla){
    if(buttonzilla == startButton){
    while(i < 7){
    story.append("\n" + "\n" + abc);
    i++;
    However, when i click the button once, it displays all elements in the array, however, im trying to get it so that each time i click the button another element is displayed, one at a time. How would i go about doing this?

    Can you explain the code because it dosent make sense
    to me, im a beginner.In the code you posted, there is no declaration for the variable i, so I've assumed it is declared as a class variable, and has scope within the class.
    You say you want to add only the data from the element of abc[], relative to the number of times the button has been clicked i.e. 1st click adds abc[0], 2nd click adds abc[1] etc.
    if(i < abc.length)  - to ensure no ArrayIndexOutOfBoundsException (really should also check i is not negative)
    story.append("\n" + "\n" + abc[i++]);
    instead of
    story.append("\n" + "\n" + abc);
    i++;

Maybe you are looking for

  • Can´t see my files in the desktop

    My files on the desktop dissapear but when i search them into spotlight i find them but when try to open, it didn´t open, any idea how to fix it?. Ticolandia

  • Sync with Photoshop Elements 6.0?

    Does anyone know whether iTunes / Apple TV supports Photoshop Elements 6.0? I upgraded to Photoshop Elements 6.0 yesterday, and now when I select "Photoshop Elements" on the Photos tab of the Appple TV in iTunes 7.4.3, no albums show up (whereas with

  • Ios 8.0.2 list of issues on my iphone 5s

    I Have the following problems: 1. Random capitalizations as you can see above. 2. Shortcuts in Safari causes a crash. I have "em" and "emm" as 2 different shortcuts. But just by typing that, safari crashes. 3. All of my apps crash. Even settings and

  • Condition check in PO

    Hi guys, I've a problem: I have a condition type defined "Required" in calculation schema and the condition appear in PO with value zero. How can I do to check if the value is greater than zero ? Regards Alberto

  • OBN Business Object

    Dear Experts. In this moment I have the following issue in the MSS=>Requisition Monitor. The system display the following message of error: There is no iview is available for system 'SAP_ERP_TalentManagement' I found in the forum http://forums.sdn.sa