Hey guys, I've been practising my C before the new Uni semester starts and I'm having a bit of trouble with this program,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
void shift(char string[], int len);
void print(char string[], int len);
int main(int argc, char *argv[]){
char string[MAX];
int input;
printf("How much would you like to shift by?:");
scanf("%d", &input);
printf("Now input your string: ");
scanf("%99s", string);
shift(string, input);
print(string, input);
getchar();
return 0;
}
void shift(char string[], int len){
int i;
for(i=0;i<strlen(string);i++){
if(string[i] > 'z' || (string[i] < 'a' && string[i] > 'Z') || string[i] < 'A'){
string[i]=string[i];
}else{
string[i] = string[i]+len;
if(string[i] > 'z' || (string[i] < 'a' && string[i] > 'Z')){
string[i] = string[i]+len-25;
}
}
}
}
void print(char string[], int len){
printf("\nYour string when shifted %d places is: %s\n", len, string);
}
| |
Firstly, when I use "fgets(string, MAX, stdin), instead of "scanf("%99s", string", it totally skips the user input when it compiles, and I don't know why.
The second problem lies in that some inputs don't come out the way I like, for example;
"Hello!" -- when shifted 1 places comes out as "Ifmmp!" which is to be expected.
"Cry "Havoc!" and let slip the Dogs of War." Comes out " -- -- when shifted 5 comes out as "hwj" --- I'm guessing this would run better with "fgets"
"IBM" -- when shifted 25 places is "b[f" -- I can see why this happens I just can't figure out a way to fix it.
Would be great if I could get feedback on either of these two problems, I'm exhausting enough time as is.