【问题标题】:function invertNumerSubstrings didn´t work [closed]函数 invertNumerSubstrings 不起作用[关闭]
【发布时间】:2014-10-24 18:44:51
【问题描述】:

我在 Ansi C 中遇到了这个问题

3 创建一个函数,该函数接收每个解码的 20 个字符的字符串数组,考虑以下几点: 一种。从左到右阅读,数字表示从那里要投资多少个字符(要投资的字符之间可以是数字,大意是, 被认为是常见字符)。
湾。数字字符应替换为反转字符串的第一个字符。

示例。字符串 aj5pr2*dfkl3abc2qwe1azk 必须是 ajd*2rpfklcbawqeazk

使用符号和指针算术

#include <stdio.h>
#include <string.h>

#define TAM 20


char* invertNumerSubstrings(char*);

int main()
{
    char chain[TAM];
    printf("\n Ingrese chain: ");
    gets(chain);
    fflush(stdin);
    char result;
    result=invertNumerSubstrings(chain);
    printf("\n Chain modified: ");
    puts(chain);
    printf("\n");   
    return 0;
}

char* invertNumerSubstrings(char* chain)
{
    int flag =0;
    char *pl= chain;
    char *pe= chain;
    char aux;
    while(*pl=='\0'&& *pe=='\0');
    {
        if(!(*pl=='1')&&(*pe=='9'))
        {
            pl++;
            pe++;
        }
        else
        {

            if(flag ==0)
            {
                pe=*pl;
                flag=1;
                pl--;
            }
            if(*pe<*pl)
            {
                aux=*pl;
                *pl=*pe;
                *pe=aux;
            }
        }
    }
    return *chain;
}

这个程序没有编译错误但没有工作

【问题讨论】:

    标签: c string char ansi-c


    【解决方案1】:

    您的代码中存在许多问题。指点其中的一些。在函数main()

    char result;
    result=invertNumerSubstrings(chain);
    

    函数invertNumerSubstrings的返回类型是char*,它与result的类型不匹配。

    while(*pl=='\0'&& *pe=='\0');
    

    上述语句中的;在逻辑上不正确,如果条件满足,可能会导致循环无限执行。 *pl=='\0'&amp;&amp; *pe=='\0' 根据问题的需要,条件看起来并不完美(如果我错了,请纠正我)。

    return *chain; 
    

    return 语句是函数invertNumerSubstrings 的最后一条语句,其返回类型与char* 不匹配。

    要获得所需的输出,您可以尝试以下操作:

    void invertNumerSubstrings(char* chain)
    {
    
    char *pl= chain;
    char* chain_ptr=chain;   // chain_ptr to hold starting address of chain
    char* final=(char*)malloc(sizeof(chain));
    char* final_ptr=final;  // // final_ptr to hold starting address of final
    memset(final, '\0', sizeof(chain));
    
    while(*pl!='\0')
    {
    
        if(*pl>=49 && *pl<=57) //
        {   
             int shift=*pl-48; // to find the shift amount
             int i=0;
             *pl++;
    
             for(i=shift-1; i>=0; i--){
                 *final=*(pl+i);
                 final++;
             }
           pl=pl+shift;  // seek the pointer to correct position            
        }
    
         else
             {
                  *final=*pl;
                   pl++;
                   final++;
    
             }
    }
    
    chain=chain_ptr; // assign original address of chain to chain again
    
    while(*final_ptr !='\0'){
          *chain=*final_ptr ;
          final_ptr++;
          chain++;             
    }
    *chain='\0';
    
    free(final);
    
    }
    

    假设:当字符串中遇到整数时,其后续字符串的长度至少等于整数值。

    【讨论】:

      【解决方案2】:

      当我尝试它时会有编译器警告 - 在 main() 你已经声明

      char result; result=invertNumerSubstrings(chain);

      但该函数的类型为char*。但是你甚至不使用result

      更严重的是,在你放while(*pl=='\0'&amp;&amp; *pe=='\0');的函数中注意错误的尾随;如果chain没有内容,这条语句将永远执行,否则只会执行一次,之后下面的代码块将执行一次仅限。

      【讨论】:

        猜你喜欢
        • 2011-08-15
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 2020-09-05
        • 2023-03-04
        • 1970-01-01
        • 2020-08-20
        • 2022-12-19
        相关资源
        最近更新 更多