Wasin Phaijith
Wasin Phaijith

Reputation: 23

strcmp in c ( invalid conversion)

Here is problem from codeforces https://codeforces.com/problemset/problem/339/A

And this is my code which I'm stuck.

char full_string[100];
char string[100][1];
for (int i = 0; i < strlen(full_string); i++)
{
    for (int j = 0; j < 2; j++)
    {
        if (j = 0)
        {
            if (strcmp(string[i][j], string[i + 1][j]) < 0)
            {
                char temp;
                temp = string[i][j];
                string[i][j] = string[i + 1][j];
                string[i + 1][j] = temp;
            }
        }
        printf("%c", string[i][j]);
    }
}

Complier told me this one.

" Invalid conversion from 'char' to 'const char*' "

The main problem is at strcmp.It said it expected const char* but string argument is char. Should I create a new array of int which store the number. Then I'll use atoi which will convert string to integer. So I can use "if(expression)" instead of strcmp.

Upvotes: 1

Views: 239

Answers (4)

chqrlie
chqrlie

Reputation: 144780

The code has multiple problems:

  • you pass single char values to strcmp instead of pointers to char: this is incorrect and will cause undefined behavior. Just use if (full_string[i - 2] > full_string[i]) { /* swap characters */ }
  • there is no need for a 2D array char string[100][1]; just sort full_string in place.
  • compute the length of full_string just once at the beginning of the outer loop.
  • bubble sort should be fast enough so sort 50 entries in less than 2 seconds.

Here is a modified version:

#include <stdio.h>

char *sort_summands(char *str) {
    int len = strlen(str);
    int sorted = 0;
    while (!sorted) {
        sorted = 1;
        for (int i = 2; i < len; i++) {
            if (str[i - 2] > str[i]) {
                char c = str[i - 2];
                str[i - 2] = str[i];
                str[i] = c;
                sorted = 0;
            }
        }
    }
    return buf;
}

int main() {
    char buf[128];
    if (fgets(buf, sizeof buf, stdin)) {
        fputs(sort_summands(buf), stdout);
    }
    return 0;
}

Upvotes: 0

user3629249
user3629249

Reputation: 16540

the following proposed code:

  1. cleanly compiles
  2. performs a bubble sort on the passed in char array

and now, the proposed code:

#include <stdio.h> 
  
void swap( char *xp, char *yp) 
{ 
    char temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 
  
// A function to implement bubble sort 
void bubbleSort(char arr[], size_t numelements) 
{ 

    for ( size_t i = 0; i < numelements-1; i++)       
    {
       // Last i elements are already in place    
       for ( size_t j = 0; j < numelements-i-1; j++)  
       {
            if (arr[j] > arr[j+1]) 
            {
                swap(&arr[j], &arr[j+1]); 
            }
        }
    }
} 

I leave it to the OP to extract the data digits (I.E. drop all the + symbols.

I leave it to the OP to print out the results

Upvotes: 0

Minjae
Minjae

Reputation: 31

I don't think you'd need strcmp since you're trying to just compare two characters as limserhane's answer mentioned above.

if (strcmp(string[i][j], string[i + 1][j]))

should look like

if (string[i][j] < string[i + 1][j])

Plus, I can't understand what you're trying to do, as following code

char temp;
temp = string[i][j];
string[i][j] = string[i + 1][j];
string[i + 1][j] = temp;

would switch the locations of a number and a '+' operator.

Since the problem is considering students are handling single digit number, I would write a helper function that gets all the single digit numbers as character array/string, then sort, and then return it back all sorted. Then just add '+' operator wherever it needs.

Upvotes: 0

ClaudioDeLise
ClaudioDeLise

Reputation: 247

Here is the prototype of strcmp function:

int strcmp(const char *s1, const char *s2);

It compares two strings and returns 0 if the compared strings are equal. A non-zero value if string1 is bigger or smaller than string2.

If you want to compare two characters you must use a normale compare just as you do to compare int.

For example:

char a = 'a';
char b = 'b';
if (a == b) {
   printf("%c is equal to %c\n", a, b);
} else {
   printf("%c is not equal to %c\n", a, b);
} 

In this case obviously a != b

Upvotes: 1

Related Questions