-
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;
}
-
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.
-
but isn't there a more elegant way to do it!!! like using isalpha!!
-
Your capatalized letters would be recognized in isalpha.
What you have:
if(ch>='a' && ch<='z')
is short and elegant
-
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??
-
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.
-
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')
-
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
-
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!
-
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);
}
-
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;
}
-
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.
-
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;
}
-
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.
-
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?