簡體   English   中英

交流程序循環后反向輸出

[英]reverse output after loop in a c program

我做這個程序

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

int main(){
    char *str, c;
    int x = 0, y = 1;

    str = (char*)malloc(sizeof(char));

    printf("Inserisci stringa principale : ");

        while (c != '\n') {
        // read the input from keyboard standard input
        c = getc(stdin);

        // re-allocate (resize) memory for character read to be stored
        str = (char*)realloc(str, y * sizeof(char));

        // store read character by making pointer point to c
        str[x] = c;

        x++;
        y++;
        }

    str[x] = '\0'; // at the end append null character to mark end of string

    printf("\nLa stringa inserita : %s", str);

      char *sub, b;
      int w = 0, z = 1;

      sub = (char*)malloc(sizeof(char));

      printf("Immetti sottostringa da cercare : ");

          while (b != '\n') {
            // read the input from keyboard standard input
            b = getc(stdin);

            // re-allocate (resize) memory for character read to be stored
            sub = (char*)realloc(sub, z * sizeof(char));

            // store read character by making pointer point to c
            sub[w] = b;

            w++;
            z++;
          }

      sub[w] = '\0'; // at the end append null character to mark end of string

    char *p1, *p2, *p3;
    int i=0,j=0,flag=0, occurrences=0;

      p1 = str;
      p2 = sub;

      for(i = 0+1; i<strlen(str); i++)
      {
        if(*p1 == *p2)
          {
              p3 = p1;


              for(j = 0;j<strlen(sub);j++)
              {
                if(*p3 == *p2)
                {
                  p3++;p2++;
                } 
                else
                  break;
              }
              p2 = sub;
              if(j + 1 == strlen(sub))
              {
                 flag = 1;
                 occurrences = occurrences + 1;
                printf("\nnel numero di volte : %d\n",occurrences );
                printf("\nSottostringa trovata all'indice : %d\n",i );
              }

          }
        p1++; 
      }


      if(flag==0)
      {
           printf("Sottostringa non trovata");
      }
    free(str);
    free(sub);
    return (0);
    }

它將在字符串中搜索給定的子字符串,一旦找到打印該子字符串的位置和找到它的次數,例如,當前如果我的字符串是aaooaaoo並且我的子字符串或輸出輸出position,3和7和最后2個(即出現的次數),我需要先獲得第2個,然后按相反的順序獲得位置,即在這種情況下,應在第2個之前打印第7個,然后再打印3個,您該怎么做?

聲明一個數組,該數組限制為您的最大入口數,並且聲明一個變量,該變量意味着已經找到了多少個實體,請執行以下操作

const int max_entrances_amount = 1000;
int entrances_count = 0;
int entrances[max_entrances_amount];

找到子字符串的入口后,請執行以下操作:

entrances[entrances_count++] = i; // position in a string;

在完成所有操作后,只需輸出一個數組

printf("entrances: %d\n", entrances_count);
for(int i = 0; i < entrances_count; i++)
    printf("%d ", entrances[i]);

您所要做的就是簡單地使用數組存儲位置,並最后反向打印它們。

例如

if(j + 1 == strlen(sub))
{
  flag = 1;
  pos[occurrences] = i; // store the position in pos array
  occurrences = occurrences + 1;
}

...

// at the end
printf("%d", occurrences);
for (j=occurrences-1; j>=0; j--)
  printf(" %d", pos[j]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM