Just wondering is it possible to find if a sequence of numbers occurs in an element, in an array.
For example; I have long long int elements in an array which include = 22041991, 22041990, 22051991
And I would like to find, and print all elements that contain the sequences '1991'.
I have code that finds the full element, i.e if I type '22041991'. But I want to be able to pick out all elements that contain '1991' and not have to target each element in full.
All help appreciated. Thank you.
CodePudding user response:
You can use the standard library function strstr to determine whether a string contains a certain substring.
Example:
#include <stdio.h>
#include <string.h>
int main( void )
{
const char * const strings[] = { "22041991", "22041990", "22051991" };
const char * const substr = "1991";
int num_strings = sizeof strings / sizeof *strings;
for ( int i = 0; i < num_strings; i )
{
printf( "The string \"%s\" ", strings[i] );
if ( strstr( strings[i], substr ) != NULL )
printf( "DOES " );
else
printf( "DOES NOT " );
printf( "contain the substring \"%s\".\n", substr );
}
return 0;
}
This program has the following output:
The string "22041991" DOES contain the substring "1991".
The string "22041990" DOES NOT contain the substring "1991".
The string "22051991" DOES contain the substring "1991".
However, it appears that you may be attempting to compare the year, which is always stored in the zero-based character indexes 4 to 7 of the string. In that case, it would be more appropriate to only compare these characters using memcmp, instead of searching the entire string.
#include <stdio.h>
#include <string.h>
int main( void )
{
const char * const strings[] = { "22041991", "22041990", "22051991" };
const char * const target_year = "1991";
int num_strings = sizeof strings / sizeof *strings;
for ( int i = 0; i < num_strings; i )
{
printf( "The string \"%s\" ", strings[i] );
if ( strlen(strings[i]) == 8 && memcmp( strings[i] 4, target_year, 4 ) == 0 )
printf( "DOES " );
else
printf( "DOES NOT " );
printf( "contain the year \"%s\".\n", target_year );
}
return 0;
}
This program has the following output:
The string "22041991" DOES contain the year "1991".
The string "22041990" DOES NOT contain the year "1991".
The string "22051991" DOES contain the year "1991".
CodePudding user response:
One solution is to print the integers to strings and the use strstr to search for a match.
But you can also do this without using strings.
You can use the remainder operator %.
The trick is to use the correct RHS argument for the remainder.
If you are looking for a value with 1 digit, use 10.
If you are looking for a value with 2 digits use 100.
If you are looking for a value with 3 digits use 1000.
Or in general: 10^number_of_digits
Once you have calculated that argument you can go through the value to be searched.
In pseudo code:
m = 10^number_of_digits_in_value_to_search_for
while value_to_search >= value_to_search_for
{
if ((value_to_search % m) == value_to_search_for) return FOUND
value_to_search = value_to_search / 10
}
return NOT_FOUND
