change text

This is a discussion on change text within the C Programming forums, part of the General Programming Boards category; I am trying to ulter the the text with charaters defined in the switch BUT I want to leave spaces ...

  1. #1
    Unregistered
    Guest

    change text

    I am trying to ulter the the text with charaters defined in the switch BUT I want to leave spaces capital letters and puntuations unchanged. I have succeeded with teh space but I can't get it to leave punctuations and capital letters unchanged.

    Please help!!



    int main(void)
    {
    const char text[]="Hello, it is me, how are you. This is a test and only a tes bye!";

    char newtext[sizeof(plaintext)];

    int i;
    char ch;

    i=0;
    newtext[i]= '\0';

    while ((ch=text[i++])!='\0')
    {

    if(ch>='a' && ch<='z')
    {
    switch (ch)
    {
    case 'a': strcat(newtext, "l"); break;
    case 'b': strcat(newtext, "j"); break;
    case 'c': strcat(newtext, "g"); break;
    case 'd': strcat(newtext, "w"); break;
    default: strcat(newtext, "Invalid"); break;
    }
    }
    else
    if (isspace(ch))
    strcat (newtext," ");

    if (isupper(ch)) ch=toupper(ch);

    }
    printf("%s\n",encrypttext);

    return 0;
    }

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Your else statement should take care of the rest of the cases.
    else
    if (isspace(ch))
    strcat (newtext," ");
    Your code only handles spaces. Just append the rest of the cases to your string and you got your result.

    if (isupper(ch)) ch=toupper(ch);
    This line does absolute nothing.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Unregistered
    Guest
    but isn't there a more elegant way to do it!!! like using isalpha!!

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Your capatalized letters would be recognized in isalpha.

    What you have:
    if(ch>='a' && ch<='z')

    is short and elegant
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Unregistered
    Guest
    I have tried using isalpha but it does not work....I guess it is a matter of where and how I put it. any suggestions will isalpha also leave my puntuation untouched but still print them??

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The only thing isalpha does is checks if your characters are between A-Z or a-z. It'll return 0 if it doesn't fall within those ranges. You have to take care of the printing yourself.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    Unregistered
    Guest
    So does this mean that i have to write new cases in the switch to handle upper cases.

    Or if i have to use alpha instead where to I put it in my program above and do I have to get rid of if(ch>='a' && ch<='z')

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    No. You do not need any cases to handle upper case letters. Based on your explanation, just make changes to the characters that fall within the ranges a-z. Leave the rest of the characters alone. You do not want to use the isalpha function since it doesn't do what you want.

    Code:
    if within range a-z
       encode letters
    else
       dont do anything with letters
    simple as that
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  9. #9
    Unregistered
    Guest
    so I should change it to:

    else
    strcat (nextext,ch);

    however I do want the new text to change the letter to uppercase if teh origainal letter in text was an uppercase!

  10. #10
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    But you said you wanted to leave the capitalized letters unchanged. If it's a capital letter, just copy it over to your new char array. Since the original was capitalized, the new one will be too!

    >> strcat (nextext,ch);
    This is wrong. The 2nd argument must also be a string. You can do something like this:

    Code:
    char chstring[2] = {0};
    ...
    else
    {
       chstring[0] = ch;
       strcat(newtext, chstring);
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  11. #11
    Unregistered
    Guest
    I am bit confused so basically I don't need to do anything keep upper cases such as if A then I want it to change to L not l

    also to keep all punctuation intact I have added code after else but it does not look right?

    Need Help........

    Here are my codes so far:


    int main(void)
    {
    const char text[]="Hello, it is me, how are you. This is a test and only a tes bye!";

    char newtext[sizeof(plaintext)];

    int i;
    char ch;

    i=0;
    newtext[i]= '\0';

    while ((ch=text[i++])!='\0')
    {

    if(ch>='a' && ch<='z')
    {
    switch (ch)
    {
    case 'a': strcat(newtext, "l"); break;
    case 'b': strcat(newtext, "j"); break;
    case 'c': strcat(newtext, "g"); break;
    case 'd': strcat(newtext, "w"); break;
    default: strcat(newtext, "Invalid"); break;
    }
    }
    else
    if ch=text;
    strcat (newtext, text);

    }
    printf("%s\n",newtext);

    return 0;
    }

  12. #12
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    So you do want to encode capital letters. Post all your code including every encoding in the switch statement. Then we can fix your code all in one shot. It's taking too long to nitpick every small detail in your code.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  13. #13
    Unregistered
    Guest
    Here are my codes:

    All I want to knw if there is a more elegant way to leave puntuation and space untouched rather than putting them in a switch

    And if I can also change the upper case letter to uppercase encrypted character like i did in the switch by again is there a more elegant way of doing so rather than put in it in a switch.


    int main(void)
    {
    const char text[]="Hello, it is me, how are you. This is a test and only a tes bye!";

    char newtext[sizeof(plaintext)];

    int i;
    char ch;

    i=0;
    newtext[i]= '\0';

    while ((ch=text[i++])!='\0')
    {

    if(ch>='a' && ch<='z')
    {
    switch (ch)
    {
    case 'a': strcat(newtext, "l"); break;
    case 'b': strcat(newtext, "j"); break;
    case 'c': strcat(newtext, "g"); break;
    case 'd': strcat(newtext, "w"); break;
    case ' ': strcat(newtext, " "); break;
    case '.': strcat(newtext, "."); break;
    case ',': strcat(newtext, ","); break;
    case '!': strcat(newtext, "!"); break;
    case 'H': strcat(newtext, "F"); break;
    case 'h': strcat(newtext, "f"); break;
    default: strcat(newtext, "Invalid"); break;
    }
    }
    else
    if ch=text;
    strcat (newtext, text);

    }
    printf("%s\n",newtext);

    return 0;
    }

  14. #14
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I already posted what you wanted in your other thread. But here it is again:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       const char text[]="Hello, it is me, how are you. This is a test and only a tes bye!";
       char newtext[sizeof(text)] = {0};
       int i;
       char ch;
    
       i = 0;
       
       while ((ch=text[i])!='\0')
       {
          ch = tolower(ch);
          if(ch>='a' && ch<='z')
          {
             switch (ch)
             {
                case 'a': ch = 'l'; break;
                case 'b': ch = 'j'; break;
                case 'c': ch = 'g'; break;
                case 'd': ch = 'w'; break;
                case 'h': ch = 'f'; break;
                default: ch = '*'; break;
             }
             if(isupper(text[i]))
                ch = toupper(ch);
          }
          newtext[i++] = ch;
       }
       printf("%s\n",newtext);
       return 0;
    }
    That should do what you want.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  15. #15
    Unregistered
    Guest
    thanks Cshot.....it works


    but I was wondering if I want to reverse the translation to the original text rather than rewriting a new switch..is it possible to simply reverse the new text to it original text?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. change color text?
    By k4z1nh0 in forum C Programming
    Replies: 4
    Last Post: 03-15-2005, 10:41 AM
  4. help with calculating change from a dollar
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2002, 03:58 PM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21