Two instances of the string lookup operation in C++ are Shared

  • 2020-05-07 20:12:57
  • OfStack

finds the first character in a string that appears only once
topic:

      finds the first character in a string that appears only once. If abaccdeff is input, b is output.

Analysis:

      1 string stores ASCII characters with an ASCII range of no more than 255.

      therefore creates an array of 255 elements to store the number of characters in the string.

      is obtained by two traversals.

Code implementation (GCC compiled through) :


#include "stdio.h"
#include "stdlib.h"
 
// Find the first in the string 1 Only appear 1 Once the character 
char firstSingle(char * str);
 
int main(void)
{
  char str[] = "abaccdeff";
  char tmp = firstSingle(str);
  printf("%c\n",tmp);
 
  return 0;
}
 
char firstSingle(char * str)
{
  //ASCII Table has 255 Characters, create 1 a 255 The mapping array of elements starts with 0
  int asc[255] = {0};
  int i;
   
  // Traversing the string and doing the characters at the same time ASCII Values map to array subscripts to count the number of occurrences; 
  for(i=0;str[i]!='\0';i++)
    asc[str[i]]++;
 
  // Go through it again and find number one 1 A there 1 The second character is what we want 
  for(i=0;str[i]!='\0';i++)
    if(asc[str[i]] == 1)
      return str[i];
  // Otherwise return null 
  return '\0';
}

Note:

      is a common way to map values to subscripts. In some cases, array traversal is avoided.       array initialization can use the function void *memset(void *s, int ch, sizet n);       can also be implemented using Pointers.


finds the longest consecutive string of Numbers in the string
topic:

Write 1 function, its prototype is int continumax(char *outputstr,char *intputstr)

Function:

Find the longest string of consecutive digits in the string and return the length of the string,

The longest number is passed to the memory of one of the function parameters outputstr  .

For example: "abcd12345ed125ss123456789", after the first address to intputstr function returns 9,

outputstr   refers to a value of 123456789

The problem is also relatively simple, there is one point to note

Code implementation (GCC compiled through) :


#include "stdio.h"
#include "stdlib.h"
 
int continumax(char * outputstr,char * inputstr);
 
int main(void)
{
  char *in = "abcd12345ed125dd123456789";
  char *out = (char *)malloc(sizeof(char)*100);
   
  int i = continumax(out,in);
 
  printf("%d\n",i);
  printf("%s\n",out);
  return 0;
}
 
int continumax(char * outputstr, char * inputstr)
{
  int len,max,i;
  char *p;
 
  len = max = 0;
 
  // If written while(inputstr != '\0'), The longest string of digits at the end of the string cannot be processed 
  while(1)
  {  
    if(*inputstr >= '0' && *inputstr <= '9')
    {
      len++;
    }
    else
    {
      if(len >max)
      {
        max = len;
        p = inputstr - len;
      }
      len = 0;
    }
    if(*inputstr++ == 0) 
      break;
  }
 
  for(i = 0;i<max;i++)
    *outputstr++ = *p ++;
 
  *outputstr = '\0';
 
  return max;
}


Related articles: