Difference between revisions of "Code Snippet: Print All Integers in a String"

From Coder Merlin
(Reorganization)
Line 1: Line 1:
= Print All Integers in a String  =
= Print All Digits in a String  =
 
== Swift ==
<syntaxhighlight lang="swift">
let text = "ab3bk49dn3"
for character in text {
    if ("0"..."9").contains(character) {
        print(character)
    }
}
</syntaxhighlight>
 
== Python ==
<syntaxhighlight lang="python">
text = 'ab3bk49dn3'
for character in text:
    if character.isdigit():
        print(character)
</syntaxhighlight>
 
== Java ==
<syntaxhighlight lang="java">
String text = "ab3bk49dn3";
for (char character : text.toCharArray()) {
    if (Character.isDigit(character)) {
        System.out.println(character);
    }
}
</syntaxhighlight>
 
<syntaxhighlight lang="java">
// This is not the best code but it is easy to understand
 
String s = "ab3bk49dn3"; // random string
String sAsInt = ""; // string that will contain the numbers from s in order
char[] numArray = {'0','1','2','3','4','5','6','7','8','9'}; // array with numbers as chars
 
// Loop through all the chars to find the numbers
for(char character: s.toCharArray()) {
  for(char digit: numArray) {
      if(character == digit) sAsInt += x + ""; // add the digit on to the end of sAsInt
  }
}
</syntaxhighlight>
 
= Print All Digits from Standard Input =


== Swift ==
== Swift ==
Line 8: Line 53:
     if let text = line {
     if let text = line {
         for character in text {
         for character in text {
             if ("0" ... "9").contains(character) {
             if ("0"..."9").contains(character) {
                 print(character)
                 print(character)
             }
             }
Line 18: Line 63:
== Python ==
== Python ==
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
string = input()
line = input()
for char in string:
for char in line:
     if char.isdigit():
     if char.isdigit():
         print(char)
         print(char)
Line 26: Line 71:


== Java ==
== Java ==
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
// This is not the best code but it is easy to understand
Scanner scanner = new Scanner(System.in);
 
while (scanner.hasNextLine()) {
String s = "ab3bk49dn3"; // random string
    for (char character : scanner.nextLine().toCharArray()) {
String sAsInt = ""; // string that will contain the numbers from s in order
        if (Character.isDigit(character)) {
char[] numArray = {'0','1','2','3','4','5','6','7','8','9'}; // array with numbers as chars
            System.out.println(character);
 
        }
// Loop through all the chars to find the numbers
    }
for(char charecter: s.toCharArray()) {
  for(char digit: numArray) {
      if(charecter == digit) sAsInt += x + ""; // add the digit on to the end of sAsInt
  }
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:25, 27 January 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Print All Digits in a String[edit]

Swift[edit]

let text = "ab3bk49dn3"
for character in text {
    if ("0"..."9").contains(character) {
        print(character)
    }
}

Python[edit]

text = 'ab3bk49dn3'
for character in text:
    if character.isdigit():
        print(character)

Java[edit]

String text = "ab3bk49dn3";
for (char character : text.toCharArray()) {
    if (Character.isDigit(character)) {
        System.out.println(character);
    }
}
// This is not the best code but it is easy to understand

String s = "ab3bk49dn3"; // random string
String sAsInt = ""; // string that will contain the numbers from s in order
char[] numArray = {'0','1','2','3','4','5','6','7','8','9'}; // array with numbers as chars

// Loop through all the chars to find the numbers
for(char character: s.toCharArray()) {
   for(char digit: numArray) {
      if(character == digit) sAsInt += x + ""; // add the digit on to the end of sAsInt
   }
}

Print All Digits from Standard Input[edit]

Swift[edit]

var line : String?
repeat {
    line = readLine()
    if let text = line {
        for character in text {
            if ("0"..."9").contains(character) {
                print(character)
            }
        }
    }
} while line != nil

Python[edit]

line = input()
for char in line:
    if char.isdigit():
        print(char)

Source

Java[edit]

Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
    for (char character : scanner.nextLine().toCharArray()) {
        if (Character.isDigit(character)) {
            System.out.println(character);
        }
    }
}

C[edit]

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main( void ) {
  char *line;
  fgets(line, 255, stdin);

  size_t line_length = strlen(line);

  for (size_t i = 0; i < line_length; i++) {
    if (isdigit(line[i])) {
      printf("%c\n",line[i]);
    }
  }

  return 0;
}