String mehrmals wiederholen



  • Ich habe gerade ein Brett vor dem Kopf.
    Wie wiederhole ich den Satz mehrmals auf Anweisung der Eingabe?

    #include <stdio.h>
    #include <windows.h>
    
    int satz(){
     int i=0;
     char satz[60];
     int wieoft;
    
     printf("\nSatzeingabe: ");
      gets(satz);
    
      printf("\n\t     ");
       while(satz[i]){
         printf("%c",satz[i++]);
        }
      printf("\n\t     ");
        printf("\nWie oft wiederholen: ");
        scanf("%d",&wieoft); 
          for(int j=0;j<wieoft;j=j+1){
              while(satz[j]){
           printf("\n\t     %c",satz[j++]);
              }    
          }   
    }
    


  • falls das ganze c sein soll:

    #include <stdio.h>
    #include <windows.h>
    
    int satz(){
     int i=0,j;
     char satz[60];
     int wieoft;
    
     printf("\nSatzeingabe: ");
      gets(satz);
    
      printf("\n\t     ");
       while(satz[i]){
         printf("%c",satz[i++]);
        }
      printf("\n\t     ");
        printf("\nWie oft wiederholen: ");
        scanf("%d",&wieoft);
          for(j=0;j<wieoft;j=j+1){
              while(satz[j]){
           printf("\n\t     %c",satz[j++]);
              }    
          }  
    }
    

    wenns cpp sein soll

    #include <iostream>
    
    int main()
    {
       std::string satz;
       int count;
       std::cout<<"Satz bitte eingeben: "<<std::endl;
       std::cin>>satz;
       std::cout<<"Und wie oft soll der Satz bitte ausgegeben werden?"<<std::endl;
       std::cin>>count;
       for(int i=0;i<count; ++i)
       {
         std::cout<<satz<<std::endl;
       }
       return 0;
    };
    


  • @TravisG: Machst du in C extra so total hässlige Einrückungen ? 🙂



  • KasF schrieb:

    @TravisG: Machst du in C extra so total hässlige Einrückungen ? 🙂

    ja klar. ich hab an dem c code allerdings rein gar nix verändert, nur das "int i" aus der for-schleife entfernt und nach oben gepackt. ich war wohl zu faul mir den rest anzusehen 🙂


Anmelden zum Antworten