Home > OS >  how to see if a letter char *c exist in word char s[30]?
how to see if a letter char *c exist in word char s[30]?

Time:12-09

here is code and i need help to find the place of letter.The strcmp is not working and i dont now where is the proble to fix.

#include <string.h>


int main(void)
    {
    
    
    char s[30]="fiordi";
    char *c;
    int cp,i,place;

    printf("Enter char: ");
    scanf("%s",&c);

    
    for(i=0; i<6; i  ){
   
     cp=strcmp(s[i],c);  
       if( cp == 0 ){
          place=i;
       }

    }

    printf("the place is :%d",place);

    }


CodePudding user response:

#include <string.h>                                                                
#include <stdio.h>                                                                 
                                                                               
                                                                               
 int main(void)                                                                     
{                                                                                                                                                                                                                      
    char s[30]="fiordi";                                                       
    size_t s_len = strlen(s);                                                  
    int place, c;                                                              
                                                                               
    printf("Enter char: ");                                                    
    scanf("%c",&c);                                                            
                                                                                                                                                     
    for(place = 0; place < s_len; place  )                                     
        if(s[place] == (char)c)                                            
             break;                                                     
    if(place == s_len)                                                         
        printf("\nchar not found in string\n");                            
    else                                                                       
        printf("the place of char in \"%s\" is in position %d", s, place); 
                                                                               
}                                                                                  

CodePudding user response:

strcmp compares strings, not characters. Without going into the details of why it doesn't work, you could just compare the characters directly:

   if(s[i] == c[0] ){
      place=i;
   }
  • Related