Jump to content

Need help getting started on this C function

bre123

I need help getting started for this function getCharInSet. Im stuck and I dont even know how to start.

 

the function prompts the user with a string pointed to by prompt and then reads a single character from the keyboard. it then sees if the character is lower case and if it is it uppercases it. it then checks if that character is in the N approved characters given in the N-length character array appointed by L. if the character is not allowed it will print a list of allowed characters.

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

First you need to get the input and store it in a string.

Do this and have it print the output.

 

Add other functionality on top of this.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, bre123 said:

the function prompts the user with a string pointed to by prompt and then reads a single character from the keyboard.

http://www.cplusplus.com/reference/cstdio/getchar/

9 minutes ago, bre123 said:

it then sees if the character is lower case and if it is it uppercases it

http://www.cplusplus.com/doc/ascii/ shows a list of standard ASCII-codes. As you can see, the letter 'A' is 0x41 and 'a' is 0x61 -- see if you can figure out how to make use of that knowledge to check if something is or isn't uppercase.

11 minutes ago, bre123 said:

it then checks if that character is in the N approved characters given in the N-length character array appointed by L. if the character is not allowed it will print a list of allowed characters.

https://www.tutorialspoint.com/cprogramming/c_arrays.htm

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, KarathKasun said:

First you need to get the input and store it in a string.

Do this and have it print the output.

 

Add other functionality on top of this.

so far I have

char *prompt = "Please enter a character: ";
printf("%s", prompt);

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, WereCatf said:

http://www.cplusplus.com/reference/cstdio/getchar/

http://www.cplusplus.com/doc/ascii/ shows a list of standard ASCII-codes. As you can see, the letter 'A' is 0x41 and 'a' is 0x61 -- see if you can figure out how to make use of that knowledge to check if something is or isn't uppercase.

https://www.tutorialspoint.com/cprogramming/c_arrays.htm

int main()
{
char *prompt = "Please enter a character: ";
printf("%s", prompt);

int N;
  do {
    N=getchar();
    putchar (N);
  } while (N != '\0');
  return 0;
}

 

this is what I have so far, this prints whatever I input. where do i go from here

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, bre123 said:

this is what I have so far, this prints whatever I input. where do i go from here

Now compare N to the ASCII uppercase-characters in the table I gave you a link to: if N is higher than the last uppercase-character or lower than the first one, it's not an uppercase-character. Then do the same to check if it's a lowercase-character: if it's not a lowercase-character either, then it's not a letter at all.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, WereCatf said:

Now compare N to the ASCII uppercase-characters in the table I gave you a link to: if N is higher than the last uppercase-character or lower than the first one, it's not an uppercase-character. Then do the same to check if it's a lowercase-character: if it's not a lowercase-character either, then it's not a letter at all.

for(N=0; N !='\0'; N++)
    {
        if(N>='a' && N<='z')
        {
            N = N - 32;
        }
    }

 

is this used

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, bre123 said:

is this used

Almost, but not quite cigar: you already have a loop, your for-loop is both not needed and it's completely broken as well.

 int main()
{
char *prompt = "Please enter a character: ";
printf("%s", prompt);

int N;
  do {
    N=getchar();
    //Put the uppercase-/lowercase-comparison here, no need for for-loop
    putchar (N);
  } while (N != '\0');
  return 0;
} 

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, WereCatf said:

Almost, but not quite cigar: you already have a loop, your for-loop is both not needed and it's completely broken as well.


 int main()
{
char *prompt = "Please enter a character: ";
printf("%s", prompt);

int N;
  do {
    N=getchar();
    putchar (N);
    //Put the uppercase-/lowercase-comparison here, no need for for-loop
  } while (N != '\0');
  return 0;
} 

 

Okay so now if I type hello for example it says

h72e69l76l76o79

10

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, bre123 said:

Okay so now if I type hello for example it says

h72e69l76l76o79

10

You did something wrong. Here, let's see:

int main()
{
char *prompt = "Please enter a character: ";
printf("%s", prompt);

int N;
  do {
    N=getchar(); //Get one character from the user and put it in N
    if(N>='a' && N<='z') //Check if N is a lowercase-character
      {
        N = N - 32; //If N is a lowercase-character, make it into an uppercase-character
      }
  putchar (N); //Output N. Do note that it's now uppercase, if it wasn't before
  } while (N != '\0'); //Keep repeating the do-while - loop as long as N isn't zero (The NULL-character, '\0', is the same thing as the number zero)
  return 0;
}

Compare this to what you did: can you see what you did wrong?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, WereCatf said:

You did something wrong. Here, let's see:


int main()
{
char *prompt = "Please enter a character: ";
printf("%s", prompt);

int N;
  do {
    N=getchar(); //Get one character from the user and put it in N
    if(N>='a' && N<='z') //Check if N is a lowercase-character
      {
        N = N - 32; //If N is a lowercase-character, make it into an uppercase-character
      }
  putchar (N); //Output N. Do note that it's now uppercase, if it wasn't before
  } while (N != '\0'); //Keep repeating the do-while - loop as long as N isn't zero (The NULL-character, '\0', is the same thing as the number zero)
  return 0;
}

Compare this to what you did: can you see what you did wrong?

after the while part of the loop I put printf("%d", N) because I want it to print the integer value of a single character in the given input

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, bre123 said:

after the while part of the loop I put printf("%d", N) because I want it to print the integer value of a single character in the given input

Then you got exactly what you wanted; putchar prints the letter, printf prints the numeric value and then the loop repeats.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, WereCatf said:

Then you got exactly what you wanted; putchar prints the letter, printf prints the numeric value and then the loop repeats.

Im so close however now when I input "hello" I get 

H72E69L76L76O79

10

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, bre123 said:

Im so close however now when I input "hello" I get 

H72E69L76L76O79

10

Yes, you print the letter and then the numeric value, so that's what you get. What do you expect? If you print a letter first, then a number, you get a letter followed by a number.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, WereCatf said:

Yes, you print the letter and then the numeric value, so that's what you get. What do you expect? If you print a letter first, then a number, you get a letter followed by a number.

I think what Im trying to get at is when I type hello. i want it to print 72

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, bre123 said:

I think what Im trying to get at is when I type hello. i want it to print 72

You'll need to figure out the logic you want. Why do you want it to print 72? Do you want it just to print the numeric value of the first character, or do you want it to print 72 if you press 'h' or what? Also, if you want it to print that on another line, you'll need to change how the entire thing works.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, bre123 said:

so far I have

char *prompt = "Please enter a character: ";

Should be:

const char *prompt = "Please enter a character: ";

prompt points to the string literal in RO storage. If you (accidentally) modify it that'll likely cause a segfault. Making it const will catch such errors at compile time.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×