刚接触C语言,这道题实在不会

qq_46120600 2019-12-31 12:32:38
求大佬教一下怎么写
...全文
95 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-06
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html
TobeseniorME 2020-01-01
  • 打赏
  • 举报
回复
不好意思,第12行与13行的find(a,'0')应该是统一的(或者直接把1好行删除,多余了)。共同学习~
TobeseniorME 2020-01-01
  • 打赏
  • 举报
回复
主函数例子+子函数;
qq_1457346882 2019-12-31
  • 打赏
  • 举报
回复
引用 4 楼 自信男孩 的回复:
[quote=引用 2 楼 qq_1457346882 的回复:]
[quote=引用 1 楼 qq_1457346882 的回复:]
int find(char *p,char ch){
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
return -1;
}

打错了,从while开始的p都是pd[/quote]
写的有点复杂了,可以更简单一些~
如下:

int find(char *p, char ch)
{
char *tmp = p;

while (tmp) {
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}

供参考~

[/quote]
我知道,本来我是写的简化,但是写完又感觉还是应该给个最基础版的,比较容易理解。还可以让他自己简化加深印象
qq_46120600 2019-12-31
  • 打赏
  • 举报
回复
引用 5 楼 自信男孩的回复:
[quote=引用 4 楼 自信男孩 的回复:]
[quote=引用 2 楼 qq_1457346882 的回复:]
[quote=引用 1 楼 qq_1457346882 的回复:]
int find(char *p,char ch){
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
return -1;
}

打错了,从while开始的p都是pd[/quote]
写的有点复杂了,可以更简单一些~
如下:

int find(char *p, char ch)
{
char *tmp = p;

while (tmp) {
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}

供参考~

[/quote]

改一个地方:
int find(char *p, char ch)
{
char *tmp = p;

while (*tmp) { //这个地方要用*tmp,因为用于判断'\0'
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}
[/quote] 之前主函数里面getchar(ch)一直报错,谢谢啦
自信男孩 2019-12-31
  • 打赏
  • 举报
回复
引用 4 楼 自信男孩 的回复:
[quote=引用 2 楼 qq_1457346882 的回复:]
[quote=引用 1 楼 qq_1457346882 的回复:]
int find(char *p,char ch){
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
return -1;
}

打错了,从while开始的p都是pd[/quote]
写的有点复杂了,可以更简单一些~
如下:

int find(char *p, char ch)
{
char *tmp = p;

while (tmp) {
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}

供参考~

[/quote]

改一个地方:
int find(char *p, char ch)
{
char *tmp = p;

while (*tmp) { //这个地方要用*tmp,因为用于判断'\0'
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}
自信男孩 2019-12-31
  • 打赏
  • 举报
回复
引用 2 楼 qq_1457346882 的回复:
[quote=引用 1 楼 qq_1457346882 的回复:]
int find(char *p,char ch){
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
return -1;
}

打错了,从while开始的p都是pd[/quote]
写的有点复杂了,可以更简单一些~
如下:

int find(char *p, char ch)
{
char *tmp = p;

while (tmp) {
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}

供参考~

  • 打赏
  • 举报
回复
int find(char *p, char ch)
{
for (char *i = p; *i; i++)
if (*i == ch) return i - p;

return -1;
}

qq_1457346882 2019-12-31
  • 打赏
  • 举报
回复
引用 1 楼 qq_1457346882 的回复:
int find(char *p,char ch){
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
return -1;
}

打错了,从while开始的p都是pd
qq_1457346882 2019-12-31
  • 打赏
  • 举报
回复
int find(char *p,char ch){
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
return -1;
}
fighting2022 2019-12-31
  • 打赏
  • 举报
回复
引用 10 楼 自信男孩 的回复:
[quote=引用 9 楼 fighting2022 的回复:] [quote=引用 5 楼 自信男孩 的回复:] [quote=引用 4 楼 自信男孩 的回复:] [quote=引用 2 楼 qq_1457346882 的回复:] [quote=引用 1 楼 qq_1457346882 的回复:] int find(char *p,char ch){ char *pd=p; int i=0; while(*p!='\0'){ if(strncmp(p,&ch,1)==0){ return i; } else{ p++; i++; } } return -1; }
打错了,从while开始的p都是pd[/quote] 写的有点复杂了,可以更简单一些~ 如下:
int find(char *p, char ch)
{
    char *tmp = p;

    while (tmp) {
        if (*tmp == ch)
            return tmp - p;
        tmp++;
    }
    return -1;
    /*
    char *pd=p;
    int i=0;
    while(*p!='\0'){
        if(strncmp(p,&ch,1)==0){
            return i;
        }
        else{
            p++;
            i++;
        }
    }
    */
    return -1;
}
供参考~ [/quote] 改一个地方:
int find(char *p, char ch)
{
    char *tmp = p;
 
    while (*tmp) {   //这个地方要用*tmp,因为用于判断'\0'
        if (*tmp == ch)
            return tmp - p;
        tmp++;
    }
    return -1;
    /*
    char *pd=p;
    int i=0;
    while(*p!='\0'){
        if(strncmp(p,&ch,1)==0){
            return i;
        }
        else{
            p++;
            i++;
        }
    }
    */
    return -1;
}
[/quote] return tmp-p; 这里有些看不懂,可以解释一下吗? [/quote] 首先建议用我的程序测试一下,如果正确再继续问,如果不对,那么就不是tmp -p的问题了。 tmp - p是tmp相对p的偏移个数。因为tmp++;每次向后偏移一个char,当出现c时,偏移tmp - p个数,就是c存在的位置。[/quote] 我试过了,没有问题,谢谢你的指点了。
自信男孩 2019-12-31
  • 打赏
  • 举报
回复
引用 9 楼 fighting2022 的回复:
[quote=引用 5 楼 自信男孩 的回复:]
[quote=引用 4 楼 自信男孩 的回复:]
[quote=引用 2 楼 qq_1457346882 的回复:]
[quote=引用 1 楼 qq_1457346882 的回复:]
int find(char *p,char ch){
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
return -1;
}

打错了,从while开始的p都是pd[/quote]
写的有点复杂了,可以更简单一些~
如下:

int find(char *p, char ch)
{
char *tmp = p;

while (tmp) {
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}

供参考~

[/quote]

改一个地方:
int find(char *p, char ch)
{
char *tmp = p;

while (*tmp) { //这个地方要用*tmp,因为用于判断'\0'
if (*tmp == ch)
return tmp - p;
tmp++;
}
return -1;
/*
char *pd=p;
int i=0;
while(*p!='\0'){
if(strncmp(p,&ch,1)==0){
return i;
}
else{
p++;
i++;
}
}
*/
return -1;
}
[/quote]
return tmp-p; 这里有些看不懂,可以解释一下吗?
[/quote]
首先建议用我的程序测试一下,如果正确再继续问,如果不对,那么就不是tmp -p的问题了。

tmp - p是tmp相对p的偏移个数。因为tmp++;每次向后偏移一个char,当出现c时,偏移tmp - p个数,就是c存在的位置。
fighting2022 2019-12-31
  • 打赏
  • 举报
回复
引用 5 楼 自信男孩 的回复:
[quote=引用 4 楼 自信男孩 的回复:] [quote=引用 2 楼 qq_1457346882 的回复:] [quote=引用 1 楼 qq_1457346882 的回复:] int find(char *p,char ch){ char *pd=p; int i=0; while(*p!='\0'){ if(strncmp(p,&ch,1)==0){ return i; } else{ p++; i++; } } return -1; }
打错了,从while开始的p都是pd[/quote] 写的有点复杂了,可以更简单一些~ 如下:
int find(char *p, char ch)
{
    char *tmp = p;

    while (tmp) {
        if (*tmp == ch)
            return tmp - p;
        tmp++;
    }
    return -1;
    /*
    char *pd=p;
    int i=0;
    while(*p!='\0'){
        if(strncmp(p,&ch,1)==0){
            return i;
        }
        else{
            p++;
            i++;
        }
    }
    */
    return -1;
}
供参考~ [/quote] 改一个地方:
int find(char *p, char ch)
{
    char *tmp = p;
 
    while (*tmp) {   //这个地方要用*tmp,因为用于判断'\0'
        if (*tmp == ch)
            return tmp - p;
        tmp++;
    }
    return -1;
    /*
    char *pd=p;
    int i=0;
    while(*p!='\0'){
        if(strncmp(p,&ch,1)==0){
            return i;
        }
        else{
            p++;
            i++;
        }
    }
    */
    return -1;
}
[/quote] return tmp-p; 这里有些看不懂,可以解释一下吗?
fighting2022 2019-12-31
  • 打赏
  • 举报
回复
完整的把这道题做了一遍,发现它给出的主程序有问题,getchar()不能被使用为getchar(ch),它的函数原型为 extern int getchar (void); 我也是第一次发现这个问题,与大家分享一下。至于函数的话,楼上的答案没有问题。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧