A general outline on what to do....
Quote:
Originally Posted by
amitj93
Code:
#include <stdio.h>
#include <string.h>
//Define all Variables
char string[100];
int count;
// Prompt User to Enter String
int array[27]= {0b0001,0b001111,0b10110,0b00000,0b011,0b11011,0b001100,0b1001,0b0100,
0b11001011,0b1100100,0b00001,0b11000,0b0101,0b0010,0b001110,0b1100101000,0b1010,
0b1000,0b111,0b10111,0b110011,0b11010,0b1100101001,0b001101,0b1100101010,0b1100101011};
int main() {
do{
printf("Enter String: ");
scanf("%s", string);
for(count=0; count<=strlen(string); count++) {
if(string[count]>97 && string[count] >= 122)
string[count]=string[count]-32;
}
printf(" String to Encode: %s", string);
}
while(strcmp(string, "quit"));
}
Hey guys,
I wrote the following code so far, which asks the user to input a string less than 100 characters such as "Programming's Hard!"
I am trying to get it so that the program will be able to restate the string that the user entered above, but do three things differently:
1) switch it to all capital letters if its not already in capital,
2) remove any spaces that may be in the string
3) remove any no alphabetic symbols (i.e. '!')
the output should look like this:
Enter String: Programming's Hard!
String to Encode: PROGRAMMINGSHARD
If you guys could help me understand how to do any of the three things, i would really appreciate it, i am really lost as to how to go about it.
thanks!
create an array of the same size to hold the new string (since we know its length
will be <= the original (use strlen). Or you could just copy the characters in
order back into the original string....
starting at the first character in the string...
for all characters
Make the character lower case by AND'ing with ~0x20.
(For instance, this converts 'a' to 'A', since 'a' is 0x61 = 0b01100001 and
'A' is 0x41 = 0b01000001. Bitwise AND'ing ('&') with ~0x20 = 0b11011111 = 0xDF
will clear the bit that needs be turned from a 1 to a 0.)
If the character is alphabetic (use isalpha, or just ask is ( ch >= 'A' &&
ch <= 'Z' ), since you just turned lower case to upper case by AND'ing, you
don't need to test for >= 'a' and <= 'z'....)
then copy it to the next position in the output string (or the
original string, if you prefer, and increment the index into the new
string
(Notice that you don't do anything to the other characters since they're
not alphabetic, so don't increment your counter into the new string for
these characters or copy them to the new string.
End for
Terminate the new string with a 0x00 value.