for c# •Write pseudocode for an algorithm that will get a string of digits as in
ID: 3785038 • Letter: F
Question
for c#
•Write pseudocode for an algorithm that will get a string of digits as input, and then output a new string which "describes" the previous one using only digits. Ex. - if the initial string is "22111344", the descriptive string result would be "22311324". This is shorthand for "two 2s, three 1s, one 3, and two 4s".
THIS IS WHAT I HAVE SO FAR PLEASE HELP CLEAN IT UP
Declare required varibles
Prompt user for digits
Loop to go through each digit from users input
Every time must be reset
Have loop check to see if it’s the same as the next digit
If so add 1
If its not break loop onto the next digit
Set the result string
Explanation / Answer
The Pseudo Code is as follows :
Declare Required Variables
Prompt User for digits
Assign string to Declared Variables
For each character in Variable
IF current character is same as next character
Increment Count
Else
Store current character of array
Append count and Previous Character To output string
Reset count to 1
End IF
END LOOP
Append Last Character of Input variable to Output String
Display Output
I have also made a small C# program for better understanding.
string val ="222111344555";
string output = string.Empty;
int count = 1;
char c=val[0];
for (int i = 1; i < val.Length; i++)
{
if (c.ToString() == val[i].ToString())
count++;
else
{
c = val[i];
output += count.ToString() + val[i - 1];
count = 1;
}
}
output += count.ToString() + val[val.Length - 1];
MessageBox.Show(output);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.