Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我在Ansi C中遇到这个问题

3考虑以下因素,创建一个函数,该函数接收由20个字符组成的字符串数组,每个字符串均进行解码:
一种。从左到右阅读该数字,它指示从那里开始要投资的字符数(为此,要投资的字符之间可以是数字,
   被视为常见字符)。
b。数字字符应替换为字符串的第一个字符。

例。字符串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;
}


该程序没有编译错误,但是没有用

最佳答案

您的代码中有很多问题。指出其中一些。在功能main()

char result;
result=invertNumerSubstrings(chain);


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

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


上面的语句中的;在逻辑上是不正确的,如果满足条件,则可能导致无限执行循环。根据问题的需要,*pl=='\0'&& *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);

}


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

关于c - 函数invertNumerSubstrings无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26554300/

10-11 23:23
查看更多