Question:
write a program to read inputs like 8A,10C etc.and print the integer and alphabet parts separately?
Lis
2013-05-10 01:59:01 UTC
write a program to read inputs like 8A,10C etc.and print the integer and alphabet parts separately?
Three answers:
Guitar Guy
2013-05-12 09:03:44 UTC
In java. Only uses one loop, so more efficient than two loops. No arbitrary arrays, or other wastes of space/time. Also, a stringbuffer works in O(n) time, whereas string concatenation works in O(n^2), so that's why a stringbuffer was used. There are multiple ways of doing this algorithm. You could use a try-catch statement with Integer.parseInt(), or check ASCII codes, or maintain a list of numerical digits and check if a character is in that list. Any of these will work, just choose the best version for the language you're using.



The pseudocode for the algorithm is as follows, in case you need to translate to another language:



Separate(s):

create a new string to collect the alpha characters

for every character in the string:

....if the character is a numerical digit:

........print it on a single line

....else:

........add it to the string

print the string on a new line





/**

* In java. Prints numbers first on one line,

* then alpha characters on the next line,

* both in the order they appear.

**/

public static void separate(String s) {

StringBuffer alphas = new StringBuffer();

alphas.append("\n");

char c;

for (int i = 0; i < s.length(); i++) {

c = s.charAt(i);

if (Character.isDigit(c)){

System.out.print(c);

}

else {

alphas.append(c);

}

}

alphas.append("\n");

System.out.print(alphas.toString());

}





Running Class.separate( "O2S7JD5GO5U0N6923O345L0N1MW3E2" ); outputs



2755069233450132

OSJDGOUNOLNMWE
Christoph
2013-05-10 02:17:55 UTC
One way to do it would be to read all input as characters and store them in an array. Then use a for loop to iterate (go through) the array picking only the numbers and printing them. After doing that repeat the process and print all of the characters. The isdigit() function should help us out on this one.



#include



using namespace std;



int main()

{

int array_size = 40;

char array[array_size];



//You might want some kind of prompt.

// This puts the user's data into the array.

cin >> array;



//print numbers

for(int i = 0; i < array_size; i++)

{

if(isdigit(array[i]))

{

cout << array[i];

}

}



//print characters

for(int i = 0; i < array_size; i++)

{

if(!isdigit(array[i]))

{

cout << array[i];

}

}

}



This is written in C++ but if you need other languages I am also good with Java and Python.
?
2016-08-04 15:34:33 UTC
Software Inverse_X(input, OutPut); $APPTYPE CONSOLE uses SysUtils; var X, I, L : Integer; S, Reverse_S : String; begin WriteLn('type a quantity'); ReadLn(X); S := IntToStr(X); // Transtype integer to String; L := length(S); Reverse_S := ''; for I := L Downto 1 do start Reverse_S := Concat(Reverse_S, S[I]); end; Writeln(Reverse_S); ReadLn; end.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'write a program to read inputs like 8A,10C etc.and print the integer and alphabet parts separately?' (Questions and Answers)
6
replies
who win the match for jonh and randy ortan?
started 2007-08-19 06:00:21 UTC
rugby league
Loading...