views:

211

answers:

4

I want to be able to have a user input a-z or c-z or c-p or whatever and have it return the actual letters between those two. I figured I would have to use ASCII numbering so I could use these steps:

1) Look for '-', if true

2) Look at first char that was input(char a in a-z), find ASCII #

3) Look at last char that was input (char z in a-z), find ASCII #

4) Print first letter based on ASCII #, then with a loop print the rest, up until the last letter in a-z (in that case being z).

Here's my code so far, but I think it's way off, I don't have a good grasp on the ASCII stuff in C, if that's what I even need.

#include <stdio.h>
#include <string.h>

void expand (char s1[], char s2[]){
    int j = 0;
    for (j; j <= s1[j-1]; ++j){
        if(s1[j+2] = '-'){
            while(j <= 70){
                ++j;
                printf("%c\n", s1[j]);
            }
        }else{
             printf("Invalid\n");
        }
    }
}

int main(){

    int g = 40;
    char s1[g], s2[g];

    printf("Please enter a-z or an equivalent:\n");

    scanf("%s", s1);
    expand(s1, s2);
    return 0;
}

Thanks for the help guys!

+2  A: 
char i;
char *str = "c-k";
for (i = str[0]; i <= str[2]; i++) {
    printf("%c", i);
}

The code doesn't do much error checking/corner conditions, but you get the idea.

If you need a resulting string instead of printing it on the screen, you can do this (assuming that you have validated str to be of the desired format):

char i;
// this will allocate memory so that the result can be stored
char *rv = (char *)malloc(((str[2] - str[0]) + 1) * sizeof(char));
for (i = str[0]; i <= str[2]; i++) {
    rv[i - str[0]] = i; // i - str[0] will result in 0, 1, 2 everytime you iterate
}
rv[i - str[0]] = '\0';

I will also recommend reading through the first few chapters of a good C-book, like K and R, so that you get a good grasp on such things.

Ashwin
+1  A: 

To begin with, you can find a table of ASCII character codes here. With these codes in hand, the operation should become relatively simple. It will go along these lines (in pseudocode form):

take_input_from_user();
parse_the_input();

for (int = first_characters_number, i =< last_characters_number; ++i)
{

    our_character = i;
    print_out(our_character);
}

For more specifics to C, IIRC you cannot pass an array like this: function_name(int[] a). IIRC, it has to go like function_name(int* a ) You can then deference the pointer and do fun stuff.

ZachS
You don't need to know the ASCII codes for this problem.
Jeanne Pindar
True, you don't need it in this problem, but I provided the reference in case he wants to do something like dealing with capital letters and the like.
ZachS
+1  A: 

The idea you posted in your question is correct, but I'm not sure how the actual code you wrote is trying to express that idea. I'd break the program down into exactly the pieces you wrote above. I've added your steps as comments in the code.

char sz[4]; // I'm assuming we're only going to get 3 characters
printf("Please enter a-z or an equivalent:\n");
scanf("%s", s1);

// You wrote: 
// 1) Look for '-', if true
// 2) Look at first char that was input(char a in a-z), find ASCII #
// 3) Look at last char that was input (char z in a-z), find ASCII #
// 
// sz[0] is the first char that was input, sz[1] the dash, sz[2] the last char
// the number of letters you need to print is sz[2] - sz[0] (+1 if you want the.
// actual letter that appears in after the dash, which you do).

int numOfLettersToPrint = sz[2] - sz[0] + 1;

// Your next step is:
// 
// 4) Print first letter based on ASCII #, then with a loop print the rest, up until the last letter in a-z (in that case being z).
for (int i = 0; i < numOfLettersToPrint; i++) {
    // Here, we want to actually print the letter.
    // sz[0] is the first letter, for example a. We add "i" to it so it goes up by one every time.
    printf("%c", sz[0] + i);
}
Edan Maor
To ensure only three characters on input, use "%.3s"!
Jonathan Leffler
A: 

This is pretty much the standard approach to this problem.

#define z 2*(c<d)-1
int d;int main(int c,char**v){c=v[1][0],d=v[1][2],d+=z;for(;c-d;c+=z)putchar(c);}
$ ./az a-e
abcde
$ ./az z-q
zyxwvutsrq
Mark Rushakoff