String to go to specific characters-2009 the computer Research of Harbin Institute of Technology The real problem

Source: Internet
Author: User


Title Description:

The input string s and the character C are required to remove all C characters from S and output the result.

Input:

The test data has multiple groups, each set of input string s and the character C.

Output:

For each set of inputs, the output removes the result after the C character.

Sample input:

Heallo
A

Sample output:

Hello

Problem Solving Code:

Solution 1: Use two arrays, the second array stores the strings that remove the specific characters

#include <stdio.h>intMain () {Chararr[ $]; Chararrnew[ $]; CharFocus;  while(SCANF ("%s", arr)! =EOF)        {GetChar (); scanf ("%c",&focus); intj =0;  for(inti =0; Arr[i]! =' /'; i++){            if(Arr[i]! =Focus) {Arrnew[j]=Arr[i]; J++; }} Arrnew[j]=' /'; printf ("%s\n", arrnew); }    return 0;}

Operation Result:

Solution 2: Use only one array, find a specific character offset once, overwrite the specific character

#include <stdio.h>#include<string.h>intMain () {Chararr[ $]; CharFocus;  while(SCANF ("%s", arr)! =EOF) {        intm =strlen (arr);        GetChar (); scanf ("%c",&focus);  for(inti =0; I < m; i++){            if(Arr[i] = =Focus) {                 for(intj = i +1; J < M; J + +) {arr[j-1] =Arr[j]; } I--; Arr[m-1] =' /'; M--; }} printf ("%s\n", arr); }    return 0;}

Operation Result:

OJ Judging situation:

Solution 1:

Solution 2:

Algorithm Analysis: (the topic is also water problem)

Solution 1: Use two arrays, the second array stores the strings that remove the specific characters

Special attention is paid to the following:

 while (scanf ("%s", arr)! = EOF) {        getchar ();        scanf ("%c", &focus);

Here's The GetChar (); Must be used, because%c, will put the enter key as a character, so that you can not enter a specific character.

Algorithmic complexity: O (n^2)

Solution 2: Use only one array, find a specific character offset once, overwrite the specific character

Note the following:

                i--;                Arr[m-1'+';                M--;

Offset once, the total string is less a character, this time is equivalent to the position of I +1, so to reduce one, the m-1 into a ' ", Explanation: The original string the last character is '", then reduce one, the second to the penultimate set to ' "".

String to go to specific characters-2009 the computer Research of Harbin Institute of Technology The real problem

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.