Finding sevens

How many times does the number '7' appear in String a?

1
String a = "1628261829271628239271982027282927827618227261434335242627282928276272728292918972625252728921011514252431524251524152524254253435241534223392028302029382002929102102929101192928101019298281910101010991929121900101010292991010101029920101092929128919010101929928912910101828272663272818182726256352562615624236252377777215252616252541256625421652392029281710000001010929298282781818288272718821827271288182712819889192919818991292191212272726167172762626216163515251.";


The answer is 32.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

15 solutions

John Taylor
Apr 12, 2015

Ctrl + F
Type 7
Count the number of times some number is highlighted

Moderator note:

Of course, you have to be careful that there are no other 7's hidden on the page. E.g. if the number of solvers is 77.

I just counted them

Oliver Blount - 6 years, 1 month ago

yuuupppzz same here

Trevor Arashiro - 6 years, 2 months ago

Doesn't apply to my phone :'(

Sherif Elbarbary - 6 years, 2 months ago

There goes my agreement.

Mark Diaz - 5 years, 8 months ago

I’m just starting and don’t know eneything

Sione Taione - 3 years, 1 month ago
Arpit MIshra
Apr 12, 2015

32 times.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
String a = "...";
int count = 0;
int len=a.length();
for(i=0;i<len;i++)
{
if(s.charAt(i)=='7')
{
count++;
}
}
System.out.println("The number of times 7 appears in this String is "+a);

Output- The number of times 7 appears in this String is 32.

I did pretty much the same thing :)

Ariel Gershon - 6 years, 2 months ago
Sid 2108
Apr 13, 2015

You can just copy the whole number in MS Words and Press Ctrl+H to open the find and replace window and replace the number "7" with anything and it shows the number of replacement done in it...

Rama Rahardi
Apr 13, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    int x = 0;
    char num[] = "1628261829271628239271982027282927827618227261434335242627282928276272728292918972625252728921011514252431524251524152524254253435241534223392028302029382002929102102929101192928101019298281910101010991929121900101010292991010101029920101092929128919010101929928912910101828272663272818182726256352562615624236252377777215252616252541256625421652392029281710000001010929298282781818288272718821827271288182712819889192919818991292191212272726167172762626216163515251";
    for (int i = 0; i < strlen(num); i++) {
        if (num[i] == '7') x++;
    }
    cout << x;
}

Arulx Z
Jun 17, 2015
1
2
String n = "...";
System.out.println(n.length() - n.replace("7", "").length());

1
'...'.count('7')

Common Python is way easier. Just see my answer.

Parth Agarwal - 5 years, 6 months ago

Log in to reply

I already posted the python solution in my answer. Check it again closely.

Arulx Z - 5 years, 6 months ago
Paulo Carlos
Apr 13, 2015

Simple.. .

Kishore S. Shenoy
Sep 16, 2015

int main()

         {

                char c[500] =         

      `"1628261829271628239271982027282927827618227261434335242627282928276272728292918972625252728921011514252431524251524152524254253435241534223392028302029382002929102102929101192928101019298281910101010991929121900101010292991010101029920101092929128919010101929928912910101828272663272818182726256352562615624236252377777215252616252541256625421652392029281710000001010929298282781818288272718821827271288182712819889192919818991292191212272726167172762626216163515251.";`

        int no = 0;

        for (int i = 0; i < strlen(c); i++) {

        if (c[i]=='7') {

         no++;

       }

     }
    std::cout<<no;

    std::getchar();

    return 0;

   }

Moderator note:

Simple standard approach.

Mauricio H
Jul 21, 2015

Javascript:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
strng  = <insert number here>;
count = 0;

for (var i in strng) {
    if (strng[i] == 7) {
        count += 1;
    }
}

document.write(count) 

Karthik Kumar
Jul 13, 2015

a = "1628261829271628239271982027282927827618227261434335242627282928276272728292918972625252728921011514252431524251524152524254253435241534223392028302029382002929102102929101192928101019298281910101010991929121900101010292991010101029920101092929128919010101929928912910101828272663272818182726256352562615624236252377777215252616252541256625421652392029281710000001010929298282781818288272718821827271288182712819889192919818991292191212272726167172762626216163515251" b=[] count=0 for i in a: b.append(i) for t in b: if t=='7': count=count+1 print count

Gavince Daña
Apr 13, 2015

Ctrl + F Type 7 Count the number of times some number is highlighted

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int ctr=0;
    string a = "1628261829271628239271982027282927827618227261434335242627282928276272728292918972625252728921011514252431524251524152524254253435241534223392028302029382002929102102929101192928101019298281910101010991929121900101010292991010101029920101092929128919010101929928912910101828272663272818182726256352562615624236252377777215252616252541256625421652392029281710000001010929298282781818288272718821827271288182712819889192919818991292191212272726167172762626216163515251.";

    for( int i=0 ; i<a.length() ; i++ )
    {
        if( a[i]=='7' )
            ctr++;
    }
    cout<<ctr<<endl;
    return 0;
}

Daniel Venable
Aug 22, 2018

Using Haskell you can do it in 2 lines!

count "" counter = counter

count string counter = if head string == head "7" then count (tail string) (counter + 1) else count (tail string) counter

Parth Agarwal
Nov 22, 2015

In Python:

z = "1628261829271628239271982027282927827618227261434335242627282928276272728292918972625252728921011514252431524251524152524254253435241534223392028302029382002929102102929101192928101019298281910101010991929121900101010292991010101029920101092929128919010101929928912910101828272663272818182726256352562615624236252377777215252616252541256625421652392029281710000001010929298282781818288272718821827271288182712819889192919818991292191212272726167172762626216163515251." x = z.count('7') print x

Vignesh Suresh
Oct 21, 2015

include<iostream.h>

void main() { char a[8000]="16282618292716282392719820272829278276182272614343352426272829282762727282929189726252527289210115142524315242515241525242542534352415342233920283020293820029291021029291011929281010192982819101010109919291219001010102929910101010299201010929291289190101019299289129101018282726632728181827262563525626156242362523777772152526162525412566254216523920292817100000010109292982827818182882727188218272712881827128198891929198189912921912122727261671727626 26216163515251";

int count=0;

for(int i=0;;i++) { if(a[i]!=0) {if(a[i]=='7')count++;} else break; }

cout<<count<<endl;

}

Agnaldo Cunha
Oct 8, 2015

Python is more elegant

def how_many_7(string):
    count = 0
    for char in string:
        if char == '7':
            count += 1
    return count

print how_many_7('1628261829271628239271982027282927827618227261434335242627282928276272728292918972625252728921011514252431524251524152524254253435241534223392028302029382002929102102929101192928101019298281910101010991929121900101010292991010101029920101092929128919010101929928912910101828272663272818182726256352562615624236252377777215252616252541256625421652392029281710000001010929298282781818288272718821827271288182712819889192919818991292191212272726167172762626216163515251.')

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...