C & C ++ common small function code

Source: Internet
Author: User

Format the input and output files

# include "stdafx. H "<br/> # include <stdlib. h> <br/> # include "string. H "<br/> # include" stdio. H "<br/> int main () <br/>{< br/> file * FP; <br/> int number [3]; <br/> int score [3]; <br/> int I = 0; <br/> int rece_number = 0; <br/> If (FP = fopen ("test.txt", "R") = NULL) <br/>{< br/> fprintf (stderr, "Error opening file! /N "); <br/>}< br/> for (I = 0; I <3; I ++) <br/>{< br/> fscanf (FP, "% d", & number [I], & score [I]); // enter a space for the student ID and score <br/>}< br/> printf ("Please input number:/N"); <br/> scanf ("% d ", & rece_number); <br/> for (I = 0; I <3; I ++) <br/> {<br/> If (rece_number = number [I]) <br/> break; <br/>}< br/> printf ("the number's score is % d/N", score [I]); <br/> fclose (FP ); <br/> return 0; <br/>}< br/>

count the longest repeated string in a string

# Include "stdafx. H "<br/> # include" string. H "<br/> # include" malloc. H "</P> <p> int main () <br/>{< br/> int I, J, K, T; <br/> int max = 0; <br/> char * P = NULL; <br/> char STR [42] = "2xtuaojy5678zuaojy908puaojy#uaojy356"; <br/> char temp [8]; <br/> char result [8]; <br/> memset (result, 0, 8); <br/> P = STR; <br/> for (I = 0; I <42; I ++) <br/>{< br/> for (j = I + 1; j <42; j ++) <br/>{< br/> If (* (p + I) = * (p + J) <br/>{< br/> T = I; <br/> K = 0; <br/> memset (temp,); <br/> // if the same first character is found, start the comparison until there are no identical characters, record the current string to see if it is the longest <br/> while (* (p + T) = * (p + J )) <br/> {<br/> temp [k] = * (p + T); // compare subsequent characters continuously <br/> T ++; <br/> J ++; <br/> K ++; <br/>}< br/> If (k> MAX) // assign the longest string to result <br/>{< br/> max = strlen (temp); <br/> strcpy (result, temp ); <br/>}</P> <p >}< br/> // print the result above the longest string is "zuaojy" <br/> printf ("the longest repeated string is % s, length: % d/N ", result, max); <br/> return 0; <br/>}

 

Recursively calculate the number of identical characters in a string

Int count_times (char CH, const char * Str) <br/>{< br/> int COUNT = 0; <br/> If (* STR = '/0 ') <br/>{< br/> COUNT = 0; <br/>}< br/> else <br/>{< br/> If (* Str) = CH) <br/>{< br/> COUNT = 1 + count_times (CH, STR + 1 ); <br/>}< br/> else <br/> {<br/> COUNT = count_times (CH, STR + 1 ); <br/>}< br/> return count; <br/>} 

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.