【问题标题】:how to see if a letter char *c exist in word char s[30]?如何查看单词 char s[30] 中是否存在字母 char *c?
【发布时间】:2022-01-13 16:56:50
【问题描述】:

这是代码,我需要帮助才能找到字母的位置。strcmp 不起作用,我现在不知道哪里有问题要解决。

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

    }


【问题讨论】:

  • 我给你写了这个例子是因为你的一段代码有不止一个问题:你将“c”的地址传递给 scanf(),它是一个未初始化的指针 (char * c),而且,总是在 scanf 中,您使用“%s”格式,该格式至少会写入您传递给它的地址 2 个字符:您输入的字符和一个 '\0' 字符(使用“%s”的字符串结尾)。你的运气是一个指针比 2 个字符宽。如果您在输入过程中写了“hello world”而不是输入一个字符,那么您的程序不仅会由于“if”而出现故障,而且会真正崩溃。

标签: arrays string if-statement char strcmp


【解决方案1】:
#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); 
                                                                               
}                                                                                  

【讨论】:

  • 你非常感谢!
【解决方案2】:

strcmp 比较字符串,而不是字符。无需详细说明为什么它不起作用,您可以直接比较字符:

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

【讨论】:

  • 非常感谢!
  • 修复可能不起作用。 OP 在没有初始化的情况下将 'c' 声明为“char *c”(c 指向任何内容),然后将其地址传递给 scanf(),因此现在指针而不是地址包含在 input + '\0' 中输入的内容: ) 事实上,如果你像这样改变它,你的修复就可以工作:if(s[i] == &c[0])。或许让 OP 更改 'c' 的数据类型和 scanf() 的格式会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多