1、
#include <stdio.h> int str_char(char x[], int key) { int len = 0; while(x[len]) len++; int i; for(i = 0; i < len; i++) { if(x[i] == key) return i; } if(i == len) return -1; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); puts("\n================="); printf("position: %d\n", str_char(str, 'c')); return 0; }
2、
#include <stdio.h> int str_char(char x[], int key) { int len = 0; while(x[len]) len++; int i = 0; while(1) { if(i == len) return -1; if(x[i] == key) return i; i++; } } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("position: %d\n", str_char(str, 'c')); return 0; }
3、
#include <stdio.h> int str_char(char x[], int key) { int len = 0; while(1) { if(x[len] == '\n') break; len++; } x[len] = key; int i = 0; while(1) { if(x[i] == key) break; i++; } return i < len ? i : -1; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("position: %d\n", str_char(str, 'c')); return 0; }
4、
#include <stdio.h> int str_char(char x[], int key) { int i = 0; while(1) { if(x[i] == key) return i; if(x[i] == '\0') return -1; i++; } } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("position: %d\n", str_char(str,'c')); return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。