code[vs]-Find substring Position-string handling-ladder Bronze

Source: Internet
Author: User

Title Description Description

Given the string A and string B, to ensure that B is a substring of a, please output B in a where the first occurrence.

Enter a description Input Description

Only one row contains two strings A and b

Output description Output Description

One-line integer only

Sample input Sample Input

ABCD BC

Sample output Sample Output

2

Data range and Tips Data Size & Hint

The length of the string does not exceed 100

Pascal user note: Two strings may contain multiple spaces

Idea: Because it is a line to enter two strings, so you have to split the string, my idea is to use a string to store the entire string, and then split into two (this is very troublesome, some clever children's shoes have thought of the side of the input side, which is fine), and then the substring in the first letter, Loop search string Inside there is no this letter, some words to determine whether all the letters after the letter is corresponding to the string one by one, with an ans to record the same number of letters, and finally determine if ans is equal to the length of the substring (that is, the substring and a small paragraph in the string exactly match), you can output the

The code is as follows:

1#include <stdio.h>2#include <string.h>3 intMain ()4 {5     Chara[101],x[101],y[101];6     intlen,i,j,o=0;7     intans=0;8 gets (a);9len=strlen (a);Ten      for(i=0; i<len;i++)//split into a string One     { A         if(a[i]!=' ') -         { -x[i]=A[i]; the         } -         Else  -         { -x[i]=' /'; +              Break; -         } +     } A      for(j=i;j<len;j++)//split into substrings at     { -         if(a[j]!=' ') -         { -y[o]=A[j]; -o++; -         } in     } -y[o]=' /';//add end to end to      for(i=0; I<strlen (x); i++) +     { -         if(x[i]==y[0])//use the first letter to find, if match, judge the letter behind the         { *              for(j=0; J<strlen (y); J + +) $             {Panax Notoginseng                 if(X[i+j]==y[j])//if the following letters match the string, ans++ -                 { theans++; +                 } A                 Else //If you encounter a mismatch, end, continue looking for the                 { +                      Break; -                 } $                 if(Ans==strlen (y))//determine if a substring is fully compliant with a paragraph in a string (that is, the substring has all been searched, which is the correct answer) $                 { -printf"%d\n", i+1);//The position of the first letter of the output substring in the string -                     return 0; the                 } -             }Wuyi         } the     }  -     return 0; Wu}

code[vs]-Find substring Position-string handling-ladder Bronze

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.