<code>#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int get_index(char c);
string selectionSort(string arr, int n);
int main(int argc, string argv[])
{
string key = argv[1];
int str_len = strlen(key);
string sorted = selectionSort(key, str_len);
string selectionSort(string arr, int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
char x = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = x;
}
return (string) arr;
}
</code>
<code>#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int get_index(char c);
string selectionSort(string arr, int n);
int main(int argc, string argv[])
{
string key = argv[1];
int str_len = strlen(key);
string sorted = selectionSort(key, str_len);
string selectionSort(string arr, int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
char x = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = x;
}
return (string) arr;
}
</code>
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int get_index(char c);
string selectionSort(string arr, int n);
int main(int argc, string argv[])
{
string key = argv[1];
int str_len = strlen(key);
string sorted = selectionSort(key, str_len);
string selectionSort(string arr, int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
char x = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = x;
}
return (string) arr;
}
I’ve got this c code and the selectionSort funcyion is updating the key variable, how could I do it so the key variable remains unchanged?
I’ve tryed to make a copy of the key but it also gets changed eaven if i set it to a const.