Application of function returning multiple values

Let’s use a programming example to introduce the application of functions returning multiple values.
Example: Write a C function that finds and prints the longest possible substring in a string consisting of the same character.
Analysis: This string is stored in a character array. To print out the longest string, you must know the character composition and length of the longest substring. That is to say, if you write a function to achieve this function, the function You have to return two values, namely the character and its length.
Method 1: Define global variables.
By defining the character and length we want to know as a global variable, and then finding these two values ​​by comparison and calculation in the function, and assigning it to the global variable we defined, although the function does not return a value, the global The value of the variable has changed.
The code to define global variable implementation is as follows.

#include <stdio.h>

int max;
char c;
void ChildString(char *p) 
{
    
      
	int stringlen=0,len=1,maxlen=1; 
	int i=0,j=1;
	char *q;
	q = p;
	while(*q!='\0') 
	{
    
    
		stringlen++;    //统计字符串长度
		q++;
	}
	while(i < stringlen)  
	{
    
    
		if(*(p+i)==*(p+j) && j < stringlen)
		{
    
      
			len++;        //统计子串长度
		}  
		else  
		{
    
      
			if(len > maxlen)      //统计最大子串长度
			{
    
      
				maxlen = len; 
				c = *(p+i);
			}          
			len = 1;
		}  
		i++;
		j++;
	}
	max = maxlen;  
}

void main()
{
    
    
	char a[100];
	printf("Please input a string: \n");
	gets(a);
	ChildString(a);
	printf("The longest child string is : \n");
	for(int i = 0; i < max; i++)
		printf("%c",c);
	printf("\n");
}

Method 2: Return a static pointer.
By defining a static data, store the two required values. Since one of the two required values ​​is an integer and the other is a character type, it is relatively troublesome to convert and store one of them, but if they are the same Type of value, this method is still better.
Of course, you can also define this static array as a global variable, and the result will be the same.
The code that defines the static pointer implementation is as follows.

#include <stdio.h>

//int a[2];
int *ChildString(char *p) 
{
    
      
	int stringlen=0,len=1,maxlen=1; 
	int i=0,j=1;
	static int a[2];  //函数执行结束时不被释放
	char c;
	char *q;
	q = p;
	while(*q!='\0') 
	{
    
    
		stringlen++;    //统计字符串长度
		q++;
	}
	while(i < stringlen)  
	{
    
    
		if(*(p+i)==*(p+j) && j < stringlen)
		{
    
      
			len++;        //统计子串长度
		}  
		else  
		{
    
      
			if(len > maxlen)      //统计最大子串长度
			{
    
      
				maxlen = len; 
				c = *(p+i);
			}          
			len = 1;
		}  
		i++;
		j++;
	}
	a[0] = c-'0';
	a[1] = maxlen;
	return a;  
}

void main()
{
    
    
	char a[100];
	printf("Please input a string: \n");
	gets(a);
	int *p;
	p = ChildString(a);
	printf("The longest child string is : \n");
	for(int i = 0; i < *(p+1); i++)
		printf("%c",*p+'0');
	printf("\n");
}

Method 3: Define the structure.
By defining a structure to store variables of different types, the advantage of defining a structure is very obvious when many values ​​need to be returned.
The code that defines the structure implementation is as follows.

# include <stdio.h>

typedef struct{
    
    
	int max;
	char c;
}Array;

Array ChildString(char *p)
{
    
    
	Array result;
	int stringlen=0,len=1,maxlen=1;
	int i=0,j=1;
	char *q;
	q = p;
	while(*q != '\0')
	{
    
    
		stringlen++;
		q++;
	}
	while(i < stringlen)
	{
    
    
		if(*(p+i)==*(p+j) && j < stringlen)
		{
    
    
			len++;
		}
		else
		{
    
    
			if(len > maxlen)
			{
    
    
				maxlen = len;
				result.c = *(p+i);
			}
			len = 1;
		}
		i++;
		j++;
	}
	result.max = maxlen;
	return result;
}

void main()
{
    
    
	Array result;
	char a[100];
	printf("Please input a string: \n");
	gets(a);
	result = ChildString(a);
	printf("The longest child string is : \n");
	for(int i = 0; i < result.max; i++)
		printf("%c",result.c);
	printf("\n");
}

The program running results are shown in the figure below.
Insert image description here
Of course, you can also modify the code in the main function, allowing the user to loop input until a specific string is entered and then exit, as shown in the figure below.
Insert image description here
The above are a few simple ways to apply functions that return multiple values. I hope this article will be helpful to you!

Guess you like

Origin blog.csdn.net/weixin_42570192/article/details/132598208