Hello,
I seem to be having some C language probs. I just need insight on how I can write this program:
a program that reads input , and can replace a char with another one(e.g if I type k, it should replace it with a) using getchar and switch.. case only.
Thanks for the help. i'm still trying to figure it out myself too.

Recommended Answers

All 8 Replies

You might want to read this, taken directly from the top of the forum. Granted, it refers to "homework help", which may or may not be the case in your situation, but the same principals apply.

I'm not looking for a quick fix. I just happened to have run out of ideas on how to do it, and I need someone to enlighten me on how it can be done.
Self-teaching sometimes have its disadvantages, I guess.
Thanks.

Post your best attempt at the problem so far.

Being able to just copy stdin to stdout without any of the substitutions would be a start at least.

Ok. This is what I have so far:

#include <stdio.h>
#include <conio.h>

main()
{
  char alp;
 alp = getchar;

switch(alp)
 {
  case 1 :                         /* this is where the confusion starts. 
                                         How do I assign a letter to the case 1, in                                          order to let it know what when a particular
                                        alp is input, it should display something?
                                    I could do it with int type, but not with char*/

  default: statement;
              break;
}

Just let me know how I can work with "char" types. I should be able to figure out the rest. Thx.

#include <stdio.h>
/*#include <conio.h>*/

int main()
{
   int alp;
   alp = getchar();

   switch ( alp )
   {
   case '1' :
      puts("one");
      break;

   default: 
      puts("default");
      break;
   }
   return 0;
}
#include <stdio.h>
/*#include <conio.h>*/

int main()
{
   int alp;
   alp = getchar();

   switch ( alp )
   {
   case '1' :
      puts("one");
      break;

   default: 
      puts("default");
      break;
   }
   return 0;
}

Thanks very much for that code. Unfortunately, that wasn't what I need. I know how to work with int, but not with char. What I need is an explanationon how to work with char, not the code itself.
Thank you for the input. It was worthwhile.

Thanks very much for that code. Unfortunately, that wasn't what I need. I know how to work with int, but not with char. What I need is an explanationon how to work with char, not the code itself.
Thank you for the input. It was worthwhile.

Working with int is exactly the same as working with char. the only difference between int and char is that a char is 1 byte, wheras an int is usually bigger.

case 1:

The above means if the input is the integer 1

case '1' :

The means if the input is the character one, not if input is the integer 1. Note the single quotes around 1 not present in the first line of this post.

Perhaps this would be clearer:

case 'A': //if input is a capital A


Case values must equate to integer values. You can not use strings (char *, char[] or any other string type) to determine case statements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.