0

If I am not allowed to use the <string.h> library, how can I easily compare values of a string. I have a data file with 6 possible values for one member of a structure. All I need to do is create a loop to count how many of each value is present in an array of structs. The problem is, I cannot figure out how to compare the value and thus when to increment the counter.

 for (i = 0; i < datasize; i++){
    if (struct.membervalue == given)
      givencount++;
    if (struct.membervalue == given2)  // But I can't compare them with the == 
      givencount2++ ;                  // because they are strings.
 }

EDIT: predefined enum that I MUST USE

 typedef enum { 
     penny = 1,
     nickel = 5,
     dime = 10,
     quarter = 25
 }changeT;

I have the value "penny" how do I compare to this or relate it?

10
  • Can't you write your own strcmp()??? Commented Jan 29, 2013 at 4:07
  • Maybe this link can help stackoverflow.com/questions/4497680/c-strcmp-source-code Commented Jan 29, 2013 at 4:08
  • I believe there is an easier answer. I have access to enum types of each value, but I do not know how to translate my raw data into those enum types. I do not believe I am supposed to do any advanced string processing Commented Jan 29, 2013 at 4:14
  • @Vlad: I thought these were strings -- that's what you asked about. Are they actually enums and not strings at all? If so, == should work fine. Commented Jan 29, 2013 at 4:18
  • I have strings, but the exercises predefines an enum. So my data is in the format of a string, but I have a pre-defined enum type that has for its domain the value that my strings represent. Commented Jan 29, 2013 at 4:20

4 Answers 4

2
bool isEqual(const char *string1, const char *string2)
{
    do
    {
        if (*string1 != *string2) return false;
        if (*string1 == 0) return true;
        ++string1;
        ++string2;
    } while (1);
}

Update: The enum doesn't change anything. You still have to identify the string "penny" before you can assign it the value for a penny.

Sign up to request clarification or add additional context in comments.

4 Comments

That was my idea as well, but I think I am supposed to do something with enum types that I have been given.
What types are membervalue and given?
So why are you bringing enums into it?
They are predefined requirnments for the problem
1

You can try the following function:

int str_cmp(const unsigned char* str1, const unsigned char* str2)
{
    int result;

    do {
        result = (int)*str1 - (int)*str2;
        str1++;
        str2++;
    } while((!result) && (*str1|*str2))

    return result;
}

Output is a positive if str1>str2, negative if str1<str2 and zero if they are equal.

1 Comment

str_cmp("","") - could be a problem?
0

Fastest one:

int strcmp(const char *s1, const char *s2) {  
  int ret = 0;  

  while (!(ret = *(unsigned char *) s1 - *(unsigned char *) s2) && *s2) 
    ++s1, ++s2;  

  if (ret < 0) {
    ret = -1;  
  } 
  else if (ret > 0) {
    ret = 1 ;
  }  

  return ret;
 }

2 Comments

What is actual difference with my answer, except result -1,0,+1 ?
i dont know but they say this was the implementation of strcmp function in the string library as i remember. Maybe difference with do-while and bitwise op.
0
/*These variants could point to invalid memmory, but dont de-reference it.*/
int isEqual(const char *string1, const char *string2)
{
    while (*string1 == *string2++)      
        if ( 0 == *string1++  )      return 1;
    return 0;
 } 

/* This variant is NULL-resistent. For both NULL return true.*/
int isEqual(const char *string1, const char *string2)
{
    if ( !string1 || !string2   )      return  string1 == string2 ;

    while (*string1 == *string2++)      
        if ( 0 == *string1++  )      return 1;
    return 0;
 } 

These are only the function to compare strings. In order to help more we need to see the code you are trying. It could be something like:

if (isEqual(data.membervalue, "penny" )   pennycount++;
else
if (isEqual(data.membervalue, "nickel")   nickelcount++;

And the enum you provided is not of great help to count. It is useful to calculate the "monetary" total.

int Total= penny * pennycount  + nickel * nickelcount ... ;

If all you need is the total, thing get simpler:

if (isEqual(data.membervalue, "penny" )   Total += penny;
else
if (isEqual(data.membervalue, "nickel")   Total += nickel;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.