Use of a constant character array

Source: Internet
Author: User

Example 1: Misplaced keyboard (Waterloo Cup)

Description: A Common typing error is to place the HandsOn the keyboard one row to the right of the correct position. So "Q" are typed as "W" and "J" is typed as "K" and soon. You are are to decode a message typed into this manner.

Input: input consists of several lines oftext. Each line could contain digits, spaces, upper case letters (except Q, a,z), or punctuation shown above [except Back-quote (' )]. Keys labelled Withwords [Tab, Backsp, control, etc.] are not represented in the input.

Output: You are are to replace each letter orpunctuation symbol by the one immediately to it left on the QWERTY Keyboardshown Abov E. Spaces in the input should is echoed in the output.

Sample input: O S, gomrypfsu/

Sample output I AM FINE today.

Analysis: 1, every read a character ch to find a programmer really want to enter the characters, is undoubtedly troublesome, and the program to achieve a cumbersome, troublesome, time-consuming. Then we'll think about it in a different direction, if we set up a constant string that is s[], and then store the characters in the keyboard order, every time we read a character, we look for it in s[, and once we find it, we return the previous character.

The code is as follows:

The first one is to write your own trouble code

#include <stdio.h>

Char*s= "1234567890-=QWERTYUIOP[]ASDFGHJKL;" Zxcvbnm,./";

int main ()

{

CHARS1[25];

Inti=0,j;

Gets (S1);

while (s1[i]!= ' ")

{

if (s1[i]== ') printf ("");

Else

for (j=0;j<49;j++)

{

if (S1[i]==s[j]) {printf ("%c", s[j-1]);

}

i++;

}

return 0;

}

The second is a reference to the great God Rewrite

#include <stdio.h>

Char *s= "1234567890-=QWERTYUIOP[]ASDFGHJKL;" Zxcvbnm,./";//constant array

int main ()

{

Charch;

Inti

while ((Ch=getchar ())!=eof)//eof End of File

{

for (i=1;s[i]&&s[i]!=ch;i++);//here '; ' is the point of no less;

if (S[i]) printf ("%c", s[i-1]), or return the character in front of it

Elseputchar (CH);//There may be spaces

}

Return0;

}

Expand:

2, palindrome string

Description: Enter a string to determine whether he is a palindrome string and a mirrored string. The input character guarantee does not contain the number 0. The palindrome string is the same as the original string after inversion, such as ABBA and madam. The so-called mirror string, is the left and right mirror after the same as the original string, such as 2S and 3AIAE. Note that it is not possible to get a legal character after each character Fu Jian image. Each character in the subject is mirrored as shown in the following illustration. (whitespace represents no mirror)

Each line entered contains a string (containing the above characters, no white space characters) to determine whether it is a palindrome string or a mirrored string (four combinations)

Analysis: The difficulty of the main point is that the mirror string of the judgement, the character of the mirror may be the letter may be the number, which undoubtedly makes it difficult to start, anyway, I was kneeling. Suddenly remember the last question (misplaced keyboard problem), why not set up a constant character array. Set up a constant character array rev[], each character to store its mirror characters, the difficulty of the subject is greatly reduced.

Directly on the code:

#include <stdio.h>

#include <string.h>

#include <ctype.h>

Char *rev= "A 3 HILJM 0 2tuvwxy51se Z 8";//constant character array, storage mirroring

Const char*msg[]={"No", "Jingxiang", "Huiwen", "Huiwenjiaojingxiang"};

Char r (Char ch)

{

if (Isalpha (CH)) return rev[ch-' A '];//a function that determines whether it is a letter

Elsereturn rev[ch-' 0 '-25];

}

int main ()

{

CHARS[30];

while (scanf ("%s", s) ==1)

{

inti=0;

Intlen=strlen (s);

Intp=1,m=1;

for (i=0;i<len/2;i++)//Symmetric comparison, once the discrepancy, then jump out

{

if (S[i]!=s[len-i-1]) m=0;//palindrome string judgment

if (R (s[i))!=s[len-1-i]) p=0;//mirror string judgment

}

printf ("%s", Msg[m*2+p]); Here corresponds to the msg "" output above, simplifies the program,

}

Return0;

3, if you also found that the problem or have a better way to communicate with each other, learn from each other, I am a beginner, I hope to communicate with you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.