Getting algorithm contest (a) language papers arrays and strings 2

Third, talk about the election race title

Examples 3-2  WERTYU

When put his hand on the keyboard, little attention will right a wrong. Thus, Q input becomes the input W, K input becomes the input J and the like. After entering a knock-out string offset (in all uppercase letters), typist would want to play the output sentence. Enter the legal guarantee, that must be the string after the dislocation. For example, the input does not appear in capital letters A.

 

Sample input:
the OS, GOMR YPFSU /
sample output:
the I AM the FINE TODAY

 

My thoughts: This typist quickly roll calf get away

With examples 3-1, may be utilized getchar () input side, the output side. But the question stuck in how to change back to the right? switch or if both too much trouble

 

The idea of the book: use constant array

#include <stdio.h>
  char S [] = " ` 1234567890- QWERTYUIOP = [] \\ ASDFGHJKL; 'ZXCVBNM, /. " ;
  int main () {
     int I, C;
     the while ((C = getchar ()) ! = the EOF) 
    { 
        for (I = . 1 ; S [I] && S [I] = C;! I ++); // characters after looking at any positions in the constant table in the 
        IF (S [I]) 
                the putchar (S [I - . 1 ]); // if found, it outputs a character before 
        the else 
            the putchar (C); 
    } 
    return  0 ; 
 }

 # Constant array does not need to specify the size, the compiler can complete the calculation

 

 Examples 3-3 palindromic word

Enter a string, the string is determined whether it is a palindromic sequence, and mirror. To ensure that no numbers 0 input string. The so-called palindromic sequence, that is, after reversing the original string and the same, and as abba madam. After all mirrored string, which is about image and original

The same series, such as 2S and 3AIAE. Note that not every character can get a legal character after the mirror. In this problem, each of the character image shown in Figure 3-3 (blank entry indicates that the character can not be obtained after a legitimate character image) 

 

Per line comprising a string (to ensure that only the character. Excluding white space), it is determined whether the mirror palindrome string and a string (4 combinations). A blank line after the output of each set of data.
Sample input:
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
sample output:
. NOTAPALINDROME - Not IS A Palindrome
. ISAPALINILAPASI - IS A Regular Palindrome
. 2A3MEAS - Using mirrored IS A String
ATOYOTA - IS A Using mirrored Palindrome.

 

Analysis: The title refers not contain blank characters - can safely be input with scanf

Constant array can be used to solve the above-mentioned examples

#include<stdio.h>
#include<string.h>
#include<ctype.h>
const char* rev = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
const char* msg[] = {"not a palindrome", "a regular palindrome", "a mirrored string","a mirrored palindrome"};

char r(char ch) {
      if(isalpha(ch))
        return rev[ch - 'A'];
      return rev[ch - '0' + 25];
}
int main() {
      char s[30];
      while(scanf("%s", s) == 1) {
            int len = strlen(s);
            int p = 1, m = 1;
            for(int i = 0; i < (len+1)/2; i++) {
                        if(s[i] != s[len-. 1 - I]) 
                        P = 0 ; // not a palindrome string 
                        IF (R & lt (S [I]) = S [len! . 1 - I]) 
                            m = 0 ; // not mirror images of string 
                        } 
            the printf ( " % S - IS% S \ n-\ n-. " , S, MSG [m * 2 + P]); 

    } 
   return  0 ; 
}

Custom function char r (char ch), ch parameter is a character, the return value is a mirror image of the character ch. This is because the constant array 26 is a front mirror each capital letter, then the digital image 10 is 1 to 9 (number 0 not occur) , it is necessary to determine alphabetic or numeric ch

isalpha determines character is a letter , there are similar idigit, isprint the like, in the ctype.h defined.

Since the ASCII code table uppercase letters, lowercase letters and numbers are continuous, if ch is an uppercase letter, the ch-'A 'is its sequence number in the alphabet (A is the number 0, B is the number 1, and so on); Similarly, if the digital ch, the ch-'0 'value itself is this number (e.g., 5' '-' 0 '= 5)

Constant array msg, in fact, this is an array of strings, that is a two-dimensional array of characters

 

 # Defined in the header file ctype.h isalpha, isdigit, isprint other tools used to determine the properties of the characters can be, but toupper, tolower other tools can be used to change the case. If ch is an uppercase letter, the ch-'A 'is its sequence number in the alphabet (A is the number 0, B is the number 1, and so on); Similarly, if ch is a number, ch-' 0 'value itself is this number

The difference between char * a = "abc" and char a [] = "abc"

 Examples 3-4  guessing game tips

 

a

Guess you like

Origin www.cnblogs.com/expedition/p/11485330.html