Question #53019

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

}
1

Expert's answer

2015-06-11T00:00:41-0400

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/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS