在c程序中循环后反向输出

时间:2017-06-19 00:29:25

标签: c string loops substring reverse

我做这个程序

#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并且我的子字符串或输出输出位置,3和7以及最后2(即出现次数)我需要先获得2然后按相反的顺序排列,即在这种情况下应该打印在7之前的第2位然后是3,你怎么能做到了吗?

2 个答案:

答案 0 :(得分:0)

声明一个数组,该数组仅限于您的最大入口数量,以及一个变量,这意味着已经找到了多少个入口,这样做

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]);

答案 1 :(得分:0)

你可以做的只是使用数组存储位置并最后反向打印出来。

例如

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]);