How to convert character

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tameem

    How to convert character

    hi my name is tameem. i am a new c programmer.
    in a c program
    i want to give input ""tameem""
    and the output will be ""xdphhp""
    in a short: the character ""a"" will be replaced by ""d""
    i can convert only a character.so how can i convert a word or a
    sentence
    my character convert program is given below:

    #include<stdio. h>
    main()
    {
    char ch;
    printf("Enter a Character=");
    ch=getchar();
    printf("%c",ch+ 3);
    }

    so please tell me how can i convert a whole sentence.
  • Nick Keighley

    #2
    Re: How to convert character

    On 7 Jul, 07:34, Tameem <etam...@gmail. comwrote:
    hi my name is tameem. i am a new c programmer.
    in a c program
    i want to give input ""tameem""
    and the output will be ""xdphhp""
    in a short: the character ""a"" will be replaced by ""d""
    i can convert only a character.so how can i convert a word or a
    sentence
    my character convert program is given below:
    you seem to be afraid of whitespace. A few spaces in your code make
    it easier to read. Correct indentation makes the logic clearer.

    #include<stdio. h>
    main()
    {
    char ch;
    printf("Enter a Character=");
    ch=getchar();
    printf("%c",ch+ 3);
    >
    }
    #include <stdio.h /* the space in front of < makes it more readable
    */

    int main (void)
    {
    char ch;

    printf ("Enter a Character=");
    fflush (stdout); /* ensure the output appears */
    ch = getchar (); /* check for an error */
    printf ("%c", ch + 3); /* need another fflush() */
    }

    this only reads and echoes a single character.
    so please tell me how can i convert a whole sentence.
    use a loop or fgets()

    --
    Nick Keighley

    -- There are two rules for success in life:
    -- Rule 1: Don't tell people everything you know.
    --

    Comment

    • rahul

      #3
      Re: How to convert character

      On Jul 7, 11:34 am, Tameem <etam...@gmail. comwrote:
      hi my name is tameem. i am a new c programmer.
      in a c program
      i want to give input ""tameem""
      and the output will be ""xdphhp""
      in a short: the character ""a"" will be replaced by ""d""
      'a' is replaced by 'x'. How come 't' is replaced by 'x'? Did you mean
      'w'?
      i can convert only a character.so how can i convert a word or a
      sentence
      my character convert program is given below:
      >
      #include<stdio. h>
      main()
      {
      char ch;
      printf("Enter a Character=");
      ch=getchar();
      printf("%c",ch+ 3);

      As already pointed out, the code-points need not be consecutive (More
      often than not, they are consecutive but it is not guaranteed). Even
      if they are, your ch + 3 will fail for 'x', 'y', 'z'.

      /* Begin rotate.c */
      #include <stdio.h>
      #include <stdlib.h>

      #define ROTATE_SUCCESS 10
      #define ROTATE_ERROR -10
      #define INPUT_LEN 30

      static int map['z' - 'a'];

      void init(void) {
      int count = 'a';
      for (; count <= 'w'; count++) {
      map[count] = count + 3;
      }
      map['x'] = 'a';
      map['y'] = 'b';
      map['z'] = 'c';
      }

      int rotate(char* input) {
      int count = 0;

      if (NULL == input) {
      return ROTATE_ERROR;
      }

      while (input[count] != NULL) {
      input[count] = map[input[count]];
      count++;
      }
      return ROTATE_SUCCESS;
      }

      int
      main(void) {
      char input[INPUT_LEN];
      fgets(input, sizeof input - 1, stdin);
      rotate(input);
      printf("%s", rotate);
      return 0;
      }


      I haven't compiled this code but I think you get the idea how to do
      it.

      Of course, the code points may not be adjacent. In that case, either
      you will have to declare a static mapping or convert each character
      individually.

      Comment

      • santosh

        #4
        Re: How to convert character

        rahul wrote:
        On Jul 7, 11:34 am, Tameem <etam...@gmail. comwrote:
        >hi my name is tameem. i am a new c programmer.
        >in a c program
        >i want to give input ""tameem""
        >and the output will be ""xdphhp""
        >in a short: the character ""a"" will be replaced by ""d""
        >
        'a' is replaced by 'x'. How come 't' is replaced by 'x'? Did you mean
        'w'?
        >
        >i can convert only a character.so how can i convert a word or a
        >sentence
        >my character convert program is given below:
        >>
        >#include<stdio .h>
        >main()
        >{
        >char ch;
        >printf("Ente r a Character=");
        >ch=getchar() ;
        >printf("%c",ch +3);
        >
        >
        As already pointed out, the code-points need not be consecutive (More
        often than not, they are consecutive but it is not guaranteed). Even
        if they are, your ch + 3 will fail for 'x', 'y', 'z'.
        >
        /* Begin rotate.c */
        #include <stdio.h>
        #include <stdlib.h>
        #define ROTATE_SUCCESS 10
        #define ROTATE_ERROR -10
        #define INPUT_LEN 30
        >
        static int map['z' - 'a'];
        You need one more element in map.
        void init(void) {
        int count = 'a';
        for (; count <= 'w'; count++) {
        map[count] = count + 3;
        You probably want:

        map[count - 'a'] = count + 3;
        }
        map['x'] = 'a';
        map['y'] = 'b';
        map['z'] = 'c';
        Ditto.
        }
        >
        int rotate(char* input) {
        int count = 0;
        >
        if (NULL == input) {
        return ROTATE_ERROR;
        }
        >
        while (input[count] != NULL) {
        The comparison should be with '\0', though NULL will work.
        input[count] = map[input[count]];
        count++;
        }
        return ROTATE_SUCCESS;
        }
        >
        int
        main(void) {
        char input[INPUT_LEN];
        fgets(input, sizeof input - 1, stdin);
        sizeof input will do since fgets will always store one character less
        than the count passed to it.
        rotate(input);
        printf("%s", rotate);
        You want to print input here.
        return 0;
        }
        >
        >
        I haven't compiled this code but I think you get the idea how to do
        it.
        >
        Of course, the code points may not be adjacent. In that case, either
        you will have to declare a static mapping or convert each character
        individually.

        Comment

        • Richard Heathfield

          #5
          Re: How to convert character

          santosh said:
          rahul wrote:
          >
          <snip>
          >>
          >int
          >main(void) {
          > char input[INPUT_LEN];
          > fgets(input, sizeof input - 1, stdin);
          >
          sizeof input will do since fgets will always store one character less
          than the count passed to it.
          Your advice is correct - i.e. passing sizeof input is just fine - but your
          explanation is not quite accurate as written.

          Rather, fgets will never store more characters than the count passed to it,
          and the last character it stores is always a null character, not a
          character taken from the input stream.

          Pictorially, if INPUT_LEN happened to be 16:

          <----------------------------16 elements----------------------->
          +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
          | r | a | n | d | o | m | f | o | o | b | a | r | b | a | z | q |
          +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
          ^
          |
          &input[0]

          stdin: Now is the time for all good men to come to the party.\n

          fgets(input, sizeof input, stdin) now does this:

          +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
          | n | o | w | | i | s | | t | h | e | | t | i | m | e |\0 |
          +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
          <----------------- 15 elements taken from stdin ---------- ^
          |
          |
          The null terminator is not read from ------------------------+
          the stream, but is stored by fgets in
          the last position to which you grant
          access.

          <snip>

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • santosh

            #6
            Re: How to convert character

            Richard Heathfield wrote:
            santosh said:
            >sizeof input will do since fgets will always store one character less
            >than the count passed to it.
            >
            Your advice is correct - i.e. passing sizeof input is just fine - but
            your explanation is not quite accurate as written.
            >
            Rather, fgets will never store more characters than the count passed
            to it, and the last character it stores is always a null character,
            not a character taken from the input stream.
            >
            Pictorially, if INPUT_LEN happened to be 16:
            >
            <----------------------------16 elements----------------------->
            +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
            | r | a | n | d | o | m | f | o | o | b | a | r | b | a | z | q |
            +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
            ^
            |
            &input[0]
            >
            stdin: Now is the time for all good men to come to the party.\n
            >
            fgets(input, sizeof input, stdin) now does this:
            >
            +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
            | n | o | w | | i | s | | t | h | e | | t | i | m | e |\0 |
            +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
            <----------------- 15 elements taken from stdin ---------- ^
            |
            |
            The null terminator is not read from ------------------------+
            the stream, but is stored by fgets in
            the last position to which you grant
            access.
            >
            <snip>
            Thanks for the correction. I agree that my wording was very sloppy.

            Comment

            • rahul

              #7
              Re: How to convert character

              On Jul 7, 3:06 pm, santosh <santosh....@gm ail.comwrote:

              Thanks for the correction. I agree that my wording was very sloppy.
              So is my whole code. Quoting from the manual:
              FGETS(P)
              FGETS(P)

              NAME
              fgets - get a string from a stream

              SYNOPSIS
              #include <stdio.h>

              char *fgets(char *restrict s, int n, FILE *restrict stream);

              DESCRIPTION
              The fgets() function shall read bytes from stream into the
              array pointed to by s, until n-1 bytes are read,
              or a <newlineis read and transferred to s, or an end-of-file
              condition is encountered. The string is then
              terminated with a null byte.

              It read n-1 bytes. So, no need for sizeof watever - 1.
              >You probably want:
              map[count - 'a'] = count + 3;
              Yes, I do.
              while (input[count] != NULL) {
              The comparison should be with '\0', though NULL will work.
              Not to start the purists debate, NULL works but to be precise, it
              should be the '\0'. So, point taken.

              Comment

              • santosh

                #8
                Re: How to convert character

                rahul wrote:
                On Jul 7, 3:06 pm, santosh <santosh....@gm ail.comwrote:
                >
                >
                >Thanks for the correction. I agree that my wording was very sloppy.
                >
                So is my whole code. Quoting from the manual:
                FGETS(P)
                FGETS(P)
                >
                NAME
                fgets - get a string from a stream
                >
                SYNOPSIS
                #include <stdio.h>
                >
                char *fgets(char *restrict s, int n, FILE *restrict stream);
                >
                DESCRIPTION
                The fgets() function shall read bytes from stream into the
                array pointed to by s, until n-1 bytes are read,
                or a <newlineis read and transferred to s, or an end-of-file
                condition is encountered. The string is then
                terminated with a null byte.
                >
                It read n-1 bytes. So, no need for sizeof watever - 1.
                >
                >>You probably want:
                >
                map[count - 'a'] = count + 3;
                Yes, I do.
                >
                > while (input[count] != NULL) {
                >
                > The comparison should be with '\0', though NULL will work.
                Not to start the purists debate, NULL works but to be precise, it
                should be the '\0'. So, point taken.
                But the code you posted is still not correct, even after all these
                corrections. Here is a corrected version. Lines that I have corrected
                or have added have a /* ++ */ comment on them.

                /* Begin rotate.c */
                #include <stdio.h>
                #include <stdlib.h>
                #include <ctype.h /* ++ */

                #define ROTATE_SUCCESS 10
                #define ROTATE_ERROR -10
                #define INPUT_LEN 30

                static int map[('z' - 'a')+1]; /* ++ */

                void init(void) {
                int count = 'a';
                for (; count <= 'w'; count++) {
                map[count - 'a'] = count + 3; /* ++ */
                }
                map['x' - 'a'] = 'a'; /* ++ */
                map['y' - 'a'] = 'b'; /* ++ */
                map['z' - 'a'] = 'c'; /* ++ */
                }

                int rotate(char* input) {
                int count = 0;

                if (NULL == input) {
                return ROTATE_ERROR;
                }

                while (input[count] != '\0' && input[count] != '\n') { /* ++ */
                if (isalpha(input[count])) { /* ++ */
                input[count] = map[input[count]-'a']; /* ++ */
                }
                count++;
                }
                return ROTATE_SUCCESS;
                }

                int
                main(void) {
                char input[INPUT_LEN];
                init(); /* ++ */
                fgets(input, sizeof input, stdin); /* ++ */
                rotate(input);
                printf("%s\n", input); /* ++ */
                return 0;
                }


                Comment

                • santosh

                  #9
                  Re: How to convert character

                  santosh wrote:
                  rahul wrote:
                  >On Jul 7, 3:06 pm, santosh <santosh....@gm ail.comwrote:
                  >>>
                  >>Thanks for the correction. I agree that my wording was very sloppy.
                  >>
                  >So is my whole code.
                  [ ... ]
                  But the code you posted is still not correct, even after all these
                  corrections. Here is a corrected version. Lines that I have corrected
                  or have added have a /* ++ */ comment on them.
                  <snip code>

                  And that code was still incorrect since it failed to deal properly with
                  uppercase input characters. Here is yet another corrected version:

                  /* Begin rotate.c */
                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <ctype.h /* ++ */

                  #define ROTATE_SUCCESS 10
                  #define ROTATE_ERROR -10
                  #define INPUT_LEN 30

                  static int map[('z' - 'a')+1]; /* ++ */

                  void init(void) {
                  int count = 'a';
                  for (; count <= 'w'; count++) {
                  map[count - 'a'] = count + 3; /* ++ */
                  }
                  map['x' - 'a'] = 'a'; /* ++ */
                  map['y' - 'a'] = 'b'; /* ++ */
                  map['z' - 'a'] = 'c'; /* ++ */
                  }

                  int rotate(char* input) {
                  int count = 0;

                  if (NULL == input) {
                  return ROTATE_ERROR;
                  }

                  while (input[count] != '\0' && input[count] != '\n') { /* ++ */
                  if (isalpha(input[count])) { /* ++ */
                  if (isupper(input[count])) { /* ++ */
                  input[count] = toupper(map[tolower(input[count])-'a']); /* ++ */
                  }
                  else input[count] = map[input[count]-'a']; /* ++ */
                  }
                  count++;
                  }
                  return ROTATE_SUCCESS;
                  }

                  int
                  main(void) {
                  char input[INPUT_LEN];
                  init(); /* ++ */
                  fgets(input, sizeof input, stdin); /* ++ */
                  rotate(input);
                  printf("%s\n", input); /* ++ */
                  return 0;
                  }

                  Comment

                  • pete

                    #10
                    Re: How to convert character

                    santosh wrote:
                    static int map[('z' - 'a')+1]; /* ++ */
                    ('z' - 'a') could be almost any integer value.
                    ('z' - 'a') equals 40 for ebdic.
                    ('z' - 'a') is also allowed to be equal to 1,
                    or even negative,
                    if anyone would want to implement such a character set.

                    --
                    pete

                    Comment

                    • santosh

                      #11
                      Re: How to convert character

                      pete wrote:
                      santosh wrote:
                      >
                      >static int map[('z' - 'a')+1]; /* ++ */
                      >
                      ('z' - 'a') could be almost any integer value.
                      ('z' - 'a') equals 40 for ebdic.
                      ('z' - 'a') is also allowed to be equal to 1,
                      or even negative,
                      if anyone would want to implement such a character set.
                      Yes. I flagged my solution as specific to character sets with the
                      desired property, and presumably, rahul did so as well. A fully
                      portable solution is an interesting exercise for the OP. It might fetch
                      him additional credits too.

                      Comment

                      • Keith Thompson

                        #12
                        Re: How to convert character

                        rahul <rahulsinner@gm ail.comwrites:
                        On Jul 7, 3:06 pm, santosh <santosh....@gm ail.comwrote:
                        [...]
                        > while (input[count] != NULL) {
                        >
                        > The comparison should be with '\0', though NULL will work.
                        Not to start the purists debate, NULL works but to be precise, it
                        should be the '\0'. So, point taken.
                        (input[count] is of type char, yes?)

                        I don't see how your statement differs from what santosh wrote.

                        NULL will work if the implementation happens to define it as 0.
                        It will *not* work if the implementation defines it as ((void*)0).

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • rahul

                          #13
                          Re: How to convert character

                          On Jul 8, 12:22 am, Keith Thompson <ks...@mib.orgw rote:
                          rahul <rahulsin...@gm ail.comwrites:
                          On Jul 7, 3:06 pm, santosh <santosh....@gm ail.comwrote:
                          [...]
                            while (input[count] != NULL) {
                          >
                            The comparison should be with '\0', though NULL will work.
                          Not to start the purists debate, NULL works but to be precise, it
                          should be the '\0'. So, point taken.
                          >
                          (input[count] is of type char, yes?)
                          >
                          I don't see how your statement differs from what santosh wrote.
                          >
                          I said "point taken", meaning to say that I agree that I should have
                          made the comparison with '\0' :-)

                          Comment

                          • rahul

                            #14
                            Re: How to convert character

                            On Jul 7, 6:04 pm, santosh <santosh....@gm ail.comwrote:
                            pete wrote:
                            santosh wrote:
                            >
                            static int map[('z' - 'a')+1];       /* ++ */
                            >
                            ('z' - 'a') could be almost any integer value.
                            ('z' - 'a') equals 40 for ebdic.
                            ('z' - 'a') is also allowed to be equal to 1,
                            or even negative,
                            if anyone would want to implement such a character set.
                            >
                            Yes. I flagged my solution as specific to character sets with the
                            desired property, and presumably, rahul did so as well. A fully
                            portable solution is an interesting exercise for the OP. It might fetch
                            him additional credits too.
                            It may, but I don't see any algorithm which can do the rotation for an
                            arbitrary character set. For argument's sake, there may not be any
                            pattern at all. 'a' may be 90, 'b' 91, then c 190....and so on and on.
                            Is it really possible to do this without taking the particular char
                            set into account?

                            Comment

                            • pete

                              #15
                              Re: How to convert character

                              rahul wrote:
                              On Jul 7, 6:04 pm, santosh <santosh....@gm ail.comwrote:
                              >pete wrote:
                              >>santosh wrote:
                              >>>static int map[('z' - 'a')+1]; /* ++ */
                              >>('z' - 'a') could be almost any integer value.
                              >>('z' - 'a') equals 40 for ebdic.
                              >>('z' - 'a') is also allowed to be equal to 1,
                              >>or even negative,
                              >>if anyone would want to implement such a character set.
                              >Yes. I flagged my solution as specific to character sets with the
                              >desired property, and presumably, rahul did so as well. A fully
                              >portable solution is an interesting exercise for the OP. It might fetch
                              >him additional credits too.
                              It may, but I don't see any algorithm which can do the rotation for an
                              arbitrary character set. For argument's sake, there may not be any
                              pattern at all. 'a' may be 90, 'b' 91, then c 190....and so on and on.
                              Is it really possible to do this without taking the particular char
                              set into account?
                              I started with Santosh's code and tried not to change too much.

                              /* Begin rotate.c */
                              #include <stdio.h>
                              #include <stdlib.h>
                              #include <string.h>
                              #include <ctype.h>

                              #define ROTATE_SUCCESS 10
                              #define ROTATE_ERROR -10
                              #define INPUT_LEN 30

                              static char map[] = "defghijklmnopq rstuvwxyzabc"
                              "DEFGHIJKLMNOPQ RSTUVWXYZABC";
                              static char letters[] = "abcdefghijklmn opqrstuvwxyz"
                              "ABCDEFGHIJKLMN OPQRSTUVWXYZ";

                              int rotate(char* input) {
                              int count = 0;
                              int rc;

                              if (NULL == input) {
                              return ROTATE_ERROR;
                              }
                              rc = input[count];
                              while ( rc != '\0' && rc != '\n') {
                              if (isalpha(rc)) {
                              input[count] = map[strchr(letters, rc) - letters];
                              }
                              count++;
                              rc = input[count];
                              }
                              return ROTATE_SUCCESS;
                              }

                              int
                              main(void) {
                              char input[INPUT_LEN];
                              fgets(input, sizeof input, stdin);
                              rotate(input);
                              puts(input);
                              return 0;
                              }

                              /* END rotate.c */



                              --
                              pete

                              Comment

                              Working...