Difference between revisions of "Code Snippet: Print All Integers in a String"
From Coder Merlin
(Added Java Scanner examples) |
(Added integer search examples) |
||
Line 27: | Line 27: | ||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 57: | Line 42: | ||
} | } | ||
} | } | ||
</syntaxhighlight> | |||
= Print All Integers in a String = | |||
== Swift == | |||
<syntaxhighlight lang="swift"> | |||
import Foundation | |||
let text = "ab3bk49dn3" | |||
let scanner = Scanner(string:text) | |||
while !scanner.isAtEnd { | |||
if let scannedInt = scanner.scanInt() { | |||
print(scannedInt) | |||
} else { | |||
scanner.scanLocation += 1 | |||
} | |||
} | |||
</syntaxhighlight> | |||
== Java == | |||
<syntaxhighlight lang="java"> | |||
String text = "ab3bk49dn3"; | |||
String pattern = "[0-9]+"; | |||
Scanner scanner = new Scanner(text); | |||
String nextDigit = scanner.findInLine(pattern); | |||
while (nextDigit != null) { | |||
System.out.println(nextDigit); | |||
nextDigit = scanner.findInLine(pattern); | |||
} | |||
scanner.close(); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 02:02, 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 Integers in a String[edit]
Swift[edit]
import Foundation
let text = "ab3bk49dn3"
let scanner = Scanner(string:text)
while !scanner.isAtEnd {
if let scannedInt = scanner.scanInt() {
print(scannedInt)
} else {
scanner.scanLocation += 1
}
}
Java[edit]
String text = "ab3bk49dn3";
String pattern = "[0-9]+";
Scanner scanner = new Scanner(text);
String nextDigit = scanner.findInLine(pattern);
while (nextDigit != null) {
System.out.println(nextDigit);
nextDigit = scanner.findInLine(pattern);
}
scanner.close();
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)
Java[edit]
String pattern = "[0-9]";
Scanner scanner = new Scanner(System.in);
String nextDigit = scanner.findInLine(pattern);
while (nextDigit != null) {
System.out.println(nextDigit);
nextDigit = scanner.findInLine(pattern);
}
scanner.close();
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;
}