I am trying to implement strcmp
and strcpy
to re-arrange names in alphabetical order and there is an issue with my name array initialization.
The state array cannot be printed out on the console as expected.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char sort(char [], char []);
int main() {
char strStates[8] = {
'Ontario', 'Quebec', 'Manitoba', 'Alberta',
'British Colombia', 'Nova Scotia', '\0'
};
char strSorted[] = { '\0' };
int x = 0;
printf("\nThe list of states before being sorted in alphabetical order: %s", strStates);
for (x = 0; x < 7; x ) {
printf("\n%s", strStates);
}
sort(strStates[x], strSorted[x]);
printf("\nThe list of states sorted alphabetically are: ");
for (x = 0; x < 4; x ) {
printf("\n%s", strStates[x]);
}
return 0;
}
char sort(char string1[], char string2[]) {
int x, y = 0;
for (x = 0; x < 3; x ) {
for (y = 1; y < 4; y ) {
if (strcmp(string1[x], string1[y]) > 0) {
strcpy(string2[y], string1[x]);
strcpy(string1[x], string1[y]);
strcpy(string[y], string2[y]);
}
}
}
}
CodePudding user response:
You declared a character array of 8 characters
char strStates[8] = {'Ontario', 'Quebec', 'Manitoba', 'Alberta','British Colombia','Nova Scotia','\0'};
and trying to initialize it with multibyte character constant as for example 'Ontario'
that have implementation defined values.
It seems you want to declare an array of string literals. In this case you should write for example
const char * strStates[] =
{
"Ontario", "Quebec", "Manitoba", "Alberta", "British Colombia","Nova Scotia"
};
Also it is a bad idea yo use magic numbers like 8
char strStates[8] = //...
or 7
for (x = 0; x < 7; x )
or 4
for (x = 0; x < 4; x )
This makes the code unclear.
You can determine the size of the declared array as shown above the following way
const char * strStates[] =
{
"Ontario", "Quebec", "Manitoba", "Alberta", "British Colombia","Nova Scotia"
};
const size_t N = sizeof( strStates ) / sizeof( *strStates );
As a result for example the for loop that outputs elements of the array can look like
puts( "The list of states before being sorted in alphabetical order:" );
for ( size_t i = 0; i < N; i ) {
puts( strStates[i] );
}
The array strSorted declared like
char strSorted[] = {'\0'};
is not used in your program. Remove the declaration.
This call of the function sort
sort(strStates[x], strSorted[x]);
does not make sense. The argument expressions have the type char
while the function expects arguments of the type char *
.
The function sort can be declared the following way
void sort( const char *[], size_t );
and called like
sort( strStates, N );
The function definition that implements the bubble sort method can look like
void sort( const char * s[], size_t n )
{
for ( size_t sorted = 0; !( n < 2 ); n = sorted )
{
for ( size_t i = sorted = 1; i < n; i )
{
if ( strcmp( s[i], s[i-1] ) < 0 )
{
const char *tmp = s[i];
s[i] = s[i-1];
s[i-1] = tmp;
sorted = i;
}
}
}
}
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
void sort( const char * s[], size_t n )
{
for (size_t sorted = 0; !( n < 2 ); n = sorted)
{
for (size_t i = sorted = 1; i < n; i )
{
if (strcmp( s[i], s[i - 1] ) < 0)
{
const char *tmp = s[i];
s[i] = s[i - 1];
s[i - 1] = tmp;
sorted = i;
}
}
}
}
int main( void )
{
const char *strStates[] =
{
"Ontario", "Quebec", "Manitoba", "Alberta", "British Colombia","Nova Scotia"
};
const size_t N = sizeof( strStates ) / sizeof( *strStates );
puts( "The list of states before being sorted in alphabetical order:" );
for (size_t i = 0; i < N; i ) {
puts( strStates[i] );
}
sort( strStates, N );
puts( "\nThe list of states sorted alphabetically are:" );
for ( size_t i = 0; i < N; i ) {
puts( strStates[i] );
}
}
The program output is
The list of states before being sorted in alphabetical order:
Ontario
Quebec
Manitoba
Alberta
British Colombia
Nova Scotia
The list of states sorted alphabetically are:
Alberta
British Colombia
Manitoba
Nova Scotia
Ontario
Quebec
CodePudding user response:
"...there is an issue with my name array initialization."
There are other issues additional to the array initialization....
Note: Single quotes in C depict a char
. Use double quotes.. Change:
'Ontario'
to
"Ontario"
everywhere.
Additionally the \0
as the last element in the array is not necessary. Each "..." string contains its own null terminator.
Array initialization: arrays must be sized to contain the number of visible characters plus one additional byte for null terminator - for the longest string in the array.
Change
char strStates[8] = {'Ontario', ..., 'Nova Scotia','\0'};
To (including 2D array notation)
char strStates[][18] = {"Ontario", ..., "Nova Scotia"};//British Columbia (17 1)
^ ^ " " "
|longest string in collection
Or to this (avoids needing to know longest string in array.)
char *strStates[] = {"Ontario", ..., "Nova Scotia"};
For the comparison question, the following is a minimal example of how you can do that, using strcmp()
implementation of compare function in conjunction with the qsort() function:
int comp(const void* a, const void* b);
int main(void)
{
char strStates[][18] = {"Ontario", "Quebec", "Manitoba", "Alberta","British Colombia","Nova Scotia"};
qsort(strStates, size, 18, comp);
return 0;
}
int comp(const void* a, const void* b) {
const char* aa = (const char*)a;
const char* bb = (const char*)b;
return strcmp(aa, bb);
}