Answer on Question# 53019, Programming, C
how can i get the right output?
#include <stdio.h>
int main()
{
char status[] = {'s', 'i', 'm', 'p', 'l', 'e'} ;
char i, x ;
printf ("Enter character to search: ");
scanf ("%c", &x);
for(i=0;i<=5;i++)
{
if (status[5] < x || status[i] >= x)
{
if (status[5] == x)
printf ("The character is at position %d in the array.", i);
else
printf ("character is not present in the array.");
break;
}
}
}Solve
#include <stdio.h>
int main()
{
char status[] = {'s', 'i', 'm', 'p', 'l', 'e'} ;
char i, x ;
int k=-1;
printf ("Enter character to search: ");
scanf ("%c", &x);
for(i=0;i<=5;i++)
{
if (status[i] == x) //for find we must check element #i
{
printf ("The character is at position %d in the array.", i);
k=i; //save to temporary variable fact, that we find character
break;
}
}
if(k==-1) //if didn't find, k been in start position: -1
printf ("Character is not present in the array.");
}http://www.AssignmentExpert.com/
Comments