finding the min string in an array of chars
up vote
0
down vote
favorite
I'm trying to find the MIN string out of a bunch of strings in a 2D array initialized with empty strings "". The user enters some strings which are then strcpy
ied and then the following method gets called.However the if statement doesn't work as expected:
void determineMIN(char strings[MAX])
char minC[MAX] = "Z"; // Highest alphabetic uppercase char in ASCII(value: 090)
int i;
for(i=0; i < MAX; i++)
if((strcmp(minC,strings[i]) >= 0) && (strcmp(minC,"") != 0))
printf("calledn");
strcpy(minC,strings[i]);
else // < 0
continue;
printf("MIN: %sn",minC);
Take this case for example: the user enters the following 3 strings "cat"
,"dog"
and "sheep"
.Considering the rest of the array is filled with "" strings shouldn't my condition work? Because it doesn't it only gets called once and then minimum is set to "".
c arrays strcpy
add a comment |
up vote
0
down vote
favorite
I'm trying to find the MIN string out of a bunch of strings in a 2D array initialized with empty strings "". The user enters some strings which are then strcpy
ied and then the following method gets called.However the if statement doesn't work as expected:
void determineMIN(char strings[MAX])
char minC[MAX] = "Z"; // Highest alphabetic uppercase char in ASCII(value: 090)
int i;
for(i=0; i < MAX; i++)
if((strcmp(minC,strings[i]) >= 0) && (strcmp(minC,"") != 0))
printf("calledn");
strcpy(minC,strings[i]);
else // < 0
continue;
printf("MIN: %sn",minC);
Take this case for example: the user enters the following 3 strings "cat"
,"dog"
and "sheep"
.Considering the rest of the array is filled with "" strings shouldn't my condition work? Because it doesn't it only gets called once and then minimum is set to "".
c arrays strcpy
Yeah you're right about that, i forgot the ! in front of the ().I probably have another mistake somewhere in there since it prints sheep as the MIN. I'll go over it again
– Stelios Papamichail
Nov 9 at 19:01
Change(strcmp(minC,"") != 0)
to(strcmp(strings[i],"") != 0)
because you want to skip ifstrings[i]
is""
notminC
.
– Osiris
Nov 9 at 19:06
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to find the MIN string out of a bunch of strings in a 2D array initialized with empty strings "". The user enters some strings which are then strcpy
ied and then the following method gets called.However the if statement doesn't work as expected:
void determineMIN(char strings[MAX])
char minC[MAX] = "Z"; // Highest alphabetic uppercase char in ASCII(value: 090)
int i;
for(i=0; i < MAX; i++)
if((strcmp(minC,strings[i]) >= 0) && (strcmp(minC,"") != 0))
printf("calledn");
strcpy(minC,strings[i]);
else // < 0
continue;
printf("MIN: %sn",minC);
Take this case for example: the user enters the following 3 strings "cat"
,"dog"
and "sheep"
.Considering the rest of the array is filled with "" strings shouldn't my condition work? Because it doesn't it only gets called once and then minimum is set to "".
c arrays strcpy
I'm trying to find the MIN string out of a bunch of strings in a 2D array initialized with empty strings "". The user enters some strings which are then strcpy
ied and then the following method gets called.However the if statement doesn't work as expected:
void determineMIN(char strings[MAX])
char minC[MAX] = "Z"; // Highest alphabetic uppercase char in ASCII(value: 090)
int i;
for(i=0; i < MAX; i++)
if((strcmp(minC,strings[i]) >= 0) && (strcmp(minC,"") != 0))
printf("calledn");
strcpy(minC,strings[i]);
else // < 0
continue;
printf("MIN: %sn",minC);
Take this case for example: the user enters the following 3 strings "cat"
,"dog"
and "sheep"
.Considering the rest of the array is filled with "" strings shouldn't my condition work? Because it doesn't it only gets called once and then minimum is set to "".
c arrays strcpy
c arrays strcpy
asked Nov 9 at 18:53
Stelios Papamichail
141115
141115
Yeah you're right about that, i forgot the ! in front of the ().I probably have another mistake somewhere in there since it prints sheep as the MIN. I'll go over it again
– Stelios Papamichail
Nov 9 at 19:01
Change(strcmp(minC,"") != 0)
to(strcmp(strings[i],"") != 0)
because you want to skip ifstrings[i]
is""
notminC
.
– Osiris
Nov 9 at 19:06
add a comment |
Yeah you're right about that, i forgot the ! in front of the ().I probably have another mistake somewhere in there since it prints sheep as the MIN. I'll go over it again
– Stelios Papamichail
Nov 9 at 19:01
Change(strcmp(minC,"") != 0)
to(strcmp(strings[i],"") != 0)
because you want to skip ifstrings[i]
is""
notminC
.
– Osiris
Nov 9 at 19:06
Yeah you're right about that, i forgot the ! in front of the ().I probably have another mistake somewhere in there since it prints sheep as the MIN. I'll go over it again
– Stelios Papamichail
Nov 9 at 19:01
Yeah you're right about that, i forgot the ! in front of the ().I probably have another mistake somewhere in there since it prints sheep as the MIN. I'll go over it again
– Stelios Papamichail
Nov 9 at 19:01
Change
(strcmp(minC,"") != 0)
to (strcmp(strings[i],"") != 0)
because you want to skip if strings[i]
is ""
not minC
.– Osiris
Nov 9 at 19:06
Change
(strcmp(minC,"") != 0)
to (strcmp(strings[i],"") != 0)
because you want to skip if strings[i]
is ""
not minC
.– Osiris
Nov 9 at 19:06
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Your problem is that you skip if minC
is equal to ""
, but you should check strings[i]
:
void determineMIN(char strings[MAX])
char minC[MAX];
int i;
strcpy(minC, strings[0]);
for(i=1; i < MAX; i++)
if((strcmp(strings[i],"") != 0) && (strcmp(strings[i], minC) < 0))
printf("calledn");
strcpy(minC,strings[i]);
else
continue;
printf("MIN: %sn",minC);
You are correct, sorry about those few mistakes it was stupid on my part. Thanks for helping me understand and fix my errors!
– Stelios Papamichail
Nov 9 at 20:05
@SteliosPapamichail You are welcome.
– Osiris
Nov 9 at 20:09
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Your problem is that you skip if minC
is equal to ""
, but you should check strings[i]
:
void determineMIN(char strings[MAX])
char minC[MAX];
int i;
strcpy(minC, strings[0]);
for(i=1; i < MAX; i++)
if((strcmp(strings[i],"") != 0) && (strcmp(strings[i], minC) < 0))
printf("calledn");
strcpy(minC,strings[i]);
else
continue;
printf("MIN: %sn",minC);
You are correct, sorry about those few mistakes it was stupid on my part. Thanks for helping me understand and fix my errors!
– Stelios Papamichail
Nov 9 at 20:05
@SteliosPapamichail You are welcome.
– Osiris
Nov 9 at 20:09
add a comment |
up vote
2
down vote
accepted
Your problem is that you skip if minC
is equal to ""
, but you should check strings[i]
:
void determineMIN(char strings[MAX])
char minC[MAX];
int i;
strcpy(minC, strings[0]);
for(i=1; i < MAX; i++)
if((strcmp(strings[i],"") != 0) && (strcmp(strings[i], minC) < 0))
printf("calledn");
strcpy(minC,strings[i]);
else
continue;
printf("MIN: %sn",minC);
You are correct, sorry about those few mistakes it was stupid on my part. Thanks for helping me understand and fix my errors!
– Stelios Papamichail
Nov 9 at 20:05
@SteliosPapamichail You are welcome.
– Osiris
Nov 9 at 20:09
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Your problem is that you skip if minC
is equal to ""
, but you should check strings[i]
:
void determineMIN(char strings[MAX])
char minC[MAX];
int i;
strcpy(minC, strings[0]);
for(i=1; i < MAX; i++)
if((strcmp(strings[i],"") != 0) && (strcmp(strings[i], minC) < 0))
printf("calledn");
strcpy(minC,strings[i]);
else
continue;
printf("MIN: %sn",minC);
Your problem is that you skip if minC
is equal to ""
, but you should check strings[i]
:
void determineMIN(char strings[MAX])
char minC[MAX];
int i;
strcpy(minC, strings[0]);
for(i=1; i < MAX; i++)
if((strcmp(strings[i],"") != 0) && (strcmp(strings[i], minC) < 0))
printf("calledn");
strcpy(minC,strings[i]);
else
continue;
printf("MIN: %sn",minC);
answered Nov 9 at 19:11
Osiris
1,589214
1,589214
You are correct, sorry about those few mistakes it was stupid on my part. Thanks for helping me understand and fix my errors!
– Stelios Papamichail
Nov 9 at 20:05
@SteliosPapamichail You are welcome.
– Osiris
Nov 9 at 20:09
add a comment |
You are correct, sorry about those few mistakes it was stupid on my part. Thanks for helping me understand and fix my errors!
– Stelios Papamichail
Nov 9 at 20:05
@SteliosPapamichail You are welcome.
– Osiris
Nov 9 at 20:09
You are correct, sorry about those few mistakes it was stupid on my part. Thanks for helping me understand and fix my errors!
– Stelios Papamichail
Nov 9 at 20:05
You are correct, sorry about those few mistakes it was stupid on my part. Thanks for helping me understand and fix my errors!
– Stelios Papamichail
Nov 9 at 20:05
@SteliosPapamichail You are welcome.
– Osiris
Nov 9 at 20:09
@SteliosPapamichail You are welcome.
– Osiris
Nov 9 at 20:09
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231747%2ffinding-the-min-string-in-an-array-of-chars%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Yeah you're right about that, i forgot the ! in front of the ().I probably have another mistake somewhere in there since it prints sheep as the MIN. I'll go over it again
– Stelios Papamichail
Nov 9 at 19:01
Change
(strcmp(minC,"") != 0)
to(strcmp(strings[i],"") != 0)
because you want to skip ifstrings[i]
is""
notminC
.– Osiris
Nov 9 at 19:06