Reputation: 2833
I.E., you enter the number 5, and the character A and the output would yield F. I have no idea how to even start to go about this, any give me a push in the right direction?
Upvotes: 2
Views: 15458
Reputation: 11
This is what I have tried. As I am a beginner, I am not sure if the codes and logic are good or bad, but it works quite well for me. If I have made some mistakes please bear with me.
#include <stdio.h>
#include <string.h>
int main() {
int k;
char s[6];
//Input for number of characters to be shifted
scanf("%d", &k);
//Input for a 5 characters long string
scanf("%5s", s);
for(int i=0; i<strlen(s); i++) {
if(s[i]+k >= 'a' && s[i]+k <= 'z')
printf("%c", s[i]+k);
//Handling overflow issue (for overflowing 1 character)
else if((s[i]+k)-'z' == 1) {
s[i] = 'a';
printf("%c", s[i]);
}
//Handling overflow issue (for overflowing more than 1 characters)
else if((s[i]+k)-'z' > 1) {
s[i] = ((s[i]+k)-'z')+('a'-1);
printf("%c", s[i]);
}
}
return 0;
}
Upvotes: 1
Reputation: 72636
char shift_char(char c, char shift)
{
if(isalpha(c)) {
if (c>='A' && c<='Z') {
return 'A' + ( (c - 'A' + shift) % 26);
} else if(c>='a' && c<='z') {
return 'a' + ( (c - 'a' + shift) % 26);
}
}
return c;
}
Upvotes: 0
Reputation: 17487
Try this:
#include <stdio.h>
char shift_char(char val, char shift)
{
val = toupper(val);
assert(isupper(val));
char arr[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return arr[ ( (toupper(val) - 'A' + shift) % 26) ];
}
You can get a little fancier if you want to preserve the case of the character. It also assumes, but does not verify shift
is non-negative. That case may cause problems with the modulus operation you will need to guard against... or better yet prevent. Still, since this is tagged as homework, that's the sort of thing you should work through.
Upvotes: 6
Reputation: 50941
Other people have pointed out that you can use ASCII. An easy way to handle wrapping is with modulus arithmetic:
char result, ch;
int offset;
... // Populate ch with the letter to be changed and offset with the number.
result = ch - 'a';
result = (result + offset) % 26; // 26 letters in the alphabet
result += 'a';
Upvotes: 0
Reputation: 108978
If you can assume ASCII, it is easier.
Characters are no more than simple numbers: only the interpretation of said numbers changes. In ASCII all letters are sequential; so the number for 'A' + 5 is the number for 'F'; 'F' - 1 is 'E' ..., ...
int ch = 'J';
ch -= 2; putchar(ch);
ch -= 3; putchar(ch);
ch += 7; putchar(ch); putchar(ch);
ch += 3; putchar(ch);
puts("");
Just pay attention to wrapping!
If you can't assume ASCII, you need to convert characters yourself. Something like:
char charr[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int ndx = 9; /* charr[9] is 'J' */
ndx -= 2; putchar(charr[ndx]);
ndx -= 3; putchar(charr[ndx]);
ndx += 7; putchar(charr[ndx]); putchar(charr[ndx]);
ndx += 3; putchar(charr[ndx]);
puts("");
Do not forget the wrapping
Upvotes: 1
Reputation: 992955
Individual characters are represented by numbers according to the ASCII code (usually). In C, if you add a number to a character, you're shifting the character down. Try:
char c = 'A';
int n = 5;
printf("%c\n", c + n);
Upvotes: 9