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 strcpyied 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 "".










share|improve this question





















  • 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














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 strcpyied 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 "".










share|improve this question





















  • 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












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 strcpyied 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 "".










share|improve this question













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 strcpyied 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 if strings[i] is "" not minC.
    – 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










  • 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















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












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);






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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);






share|improve this answer




















  • 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














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);






share|improve this answer




















  • 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












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);






share|improve this answer












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);







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo