How to change ASCII values?

I have an assignment where I have to take a phrase, capitalize it, and use the 'double' command to change all of the letters to uppercase. I'm then supposed to loop through the resulting array of letters and change the ASCII values to values like: A=0 B=1 C=2, etc. The problem is that I've tried nearly everything to do that, but so far nothing is working. I was hoping that someone could point out what I am doing wrong and point me in the right direction. Thanks!
Here is what I have so far:
function y=stringcode(k)
k=('Zoinks!')
upper('Zoinks!')
y=double('Zoinks!')
for i=1:length(k)
A=0;
B=1;
C=2;
D=3;
E=4;
F=5;
G=6;
H=7;
I=8;
J=9;
K=10;
L=11;
M=12;
N=13;
O=14;
P=15;
Q=16;
R=17;
S=18;
T=19;
U=20;
V=21;
W=22;
X=23;
Y=24;
Z=25;
end
end

Answers (2)

You are not using tthe result of the upper call.
See if:
u = upper(k)
y = double(u)
is more appropriate.

5 Comments

That's a great idea. I put it in my code but unfortunately it is still not working.
That does what you want it to do.
This is a homework assignment, so we can offer only hints, and occasionally helping with obvious problems such as I did here.
Note that your loop defines ‘A’ through ‘Z’ the number of times equal to the number of elements in ‘y’, (although you actually need to do that only once, before the loop). What it does not do is any comparisons in any of those iterations with elements of ‘y’.
I'm not quite getting what you are saying here.
for i=1:length(k)
if k(i) == double('A')
output(i) = 0;
elseif k(i) == double('B')
output(i) = 1;
elseif k(i) == double('C')
output(i) = 2;
all the way to 'Z'
end
end
I'm then supposed to loop through the resulting array of letters and change the ASCII values to values like: A=0 B=1 C=2, etc.
If you want to replace the elements of ‘y’ by their equivalents in the ‘A=0 B=1 C=2, etc.’ relation you created, you need to compare each one with the alphabetical elements of the relation. Then replace it with the respective value from the numeric equivalent. (There are more efficient ways to do this, however you are apparently supposed to loop through them, so I leave that to you.)

Sign in to comment.

I simple way of converting characters values or character vectors to apply a numerical calculation directly to the characters.
k = 'Zoinks!';
y = upper(k)-0; % ASCII values for the uppercase versions of all characters. This includes "!" as well.
% Same as k = str2double(upper(k));
Since the ASCII value of A is 65, if you want to shift the values that are returned such that A = 0 you can do this at the same time you convert to ASCII values.
y = upper(k)-65; % Shifts ASCII values at the same time as converted so that A = 0;
If you need to remove punctuation from the characters you can use regular expressions to do this.
% Replaces anything that is not A-Z with an empty string (including whitespace characters) and converts
% to shifted ASCII values.
y = regexprep(upper(k),'[^A-Z]','')-65;

6 Comments

@Allen — Please note from my Comment: ‘This is a homework assignment, so we can offer only hints, and occasionally helping with obvious problems such as I did here.
Crap, I saw his answer. I don't think I can submit my work now because I didn't figure out the answer myself.
Star, I was unware of the policy for homework assignments, and did not originally see your comment as it was marked as an older comment and hidden from view. However, I do want to thank you for bring the homework policy to my attention and will only provide direction instead of answers in the future.
@Kyle — If your University is governed by an Honor Code (such as mine was: ‘I swear on my honor that I have neither given nor received any unacknowledged aid on this (examination, assignment, etc.’), you simply need to proceed as though you did not see it, and do not include it as part of your solution.
Professors and TAs monitor MATLAB Answers, and have occasionally made their presence known.
Ok. My college does have an honor code, so I will abide by that. I have to loop through this array to find the answer anyway, so Allen's solution might not have even helped.
What frustrates me is that the project that I am working on is not hard but I am struggling so bad in this course. I don't have a programming background (this is my first programming course), but other students seem to be learning this material so much more easily. (Rant over :) )
@Allen — It is unfortunate that this is not explicitly stated anywhere. We have all had it called to our attnetion at some point.
@Kyle — That happens to us all! See Walter Roberson’s Comment for a hint that is well within the guidelines of the help we can ethically provide.

Sign in to comment.

Categories

Asked:

on 25 Jan 2020

Commented:

on 25 Jan 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!