Sample input:
hello world
a b c d

Sample output:
hello
a
world
b
c
d

Im stuck in a infinite while loop(while(j<maxStr+2)) and i need help to get out of it.Please help! Here is my code:

#include "stdafx.h"
#include <string.h>
#define MAX_STR 5

 int _tmain(int argc, _TCHAR* argv[])


        {   
            int i=0,j=0,strNum,maxStr=0,len=0;
            int progress[MAX_STR]={0};
            char str[MAX_STR][50]={0};

            printf("Number of sentences you want to enter(1-5):");
            while(scanf("%d",&strNum)<1 || strNum>MAX_STR||strNum==0||strNum<0){

                printf("Invalid input!Plese enter again:");
                fflush(stdin); 
            }

            for(i=0; i < strNum ; i++){   

            printf("\nEnter sentence #%d: ",i+1);         
            scanf(" %[^\n]s",str[i]);

            }  

            for(i = 0; i < strNum; i++) {

            len = strlen(str[i]);

            if(maxStr < len ){

               maxStr=len;

            }
        }



            while(j<maxStr+2){

                for(i=0;i<strNum;i++){

                    j=progress[i];

                    if(str[i][j]!=' '&&str[i][j]!='\0'){
                    while(str[i][j]!=' '&&str[i][j]!='\0')
                    {
                        printf("%c",str[i][j]);
                        j++;
                    }

                    printf("\n");
                    progress[i]=j+1;
                }   

            }

          }

            return 0;
        }

Recommended Answers

All 4 Replies

OK! I solve the inf loop,but now I found a new problem! When I enter more then one space between two words its not giving me the corect order.I didnt star a new discussion because I dont want to spam them

input:
hello world
a b c d

output:
hello
a
b
world
c
d

and it should be:

hello
a
world
b
c
d

#include "stdafx.h"
#include <string.h>

#define MAX_STR 5

int _tmain(int argc, _TCHAR* argv[])
{   
    int i=0,j=0,strNum,maxStr=0,len=0,c=0,d=0;
    int progress[MAX_STR]={0};
    char str[MAX_STR][50]={0},blank[50],text[50];

    printf("Number of sentences you want to enter(1-5):");
    while(scanf("%d",&strNum)<1 || strNum>MAX_STR||strNum==0||strNum<0){

        printf("Invalid input!Plese enter again:");
        fflush(stdin); 
    }

    for(i=0; i < strNum ; i++){   

    printf("\nEnter sentence #%d: ",i+1);         
    scanf(" %[^\n]s",str[i]);

    }  

    for(i = 0; i < strNum; i++) {

    len = strlen(str[i]);

    if(maxStr < len ){

       maxStr=len;

    }
}
    printf("\n");


    while(j<maxStr+4){

        for(i=0;i<strNum;i++){

            j=progress[i];

            if(str[i][j]!=' '&&str[i][j]!='\0'){
            while(str[i][j]!=' '&&str[i][j]!='\0')
            {
                printf("%c",str[i][j]);
                j++;
            }

            printf("\n");
            progress[i]=j+1;
        }
         j++;
         progress[i]=j;
    }

  }


    return 0;
}

Are you sure you solved the issue? Try this input data

hello worlds
a b c d

NOTE The added s on worlds

Does that work with your new code.

The problem is that your comparison at line 39 is effected by the assignments at lines 43 and 56 which relate to the lengths of the current strings. This means that the comparison at 39 is dependent on the relative sizes of maxString and length of str[strNum-1]. That is the data you input and the order of that data both alter the comparison at 39.

For your current data that is the difference between the length of the maximum string (hello or world both length 5) and the length of d which is why I ask you to try changing world to worlds.

The truth is this code does not properly detect the end of its loop, you need a condition that is not dependent on j which varies with the data.

The actual stop condition is that you have output all words from all strings, that is you have a complete iteration of the for loop at 41 that never enters the if block at 45.

Yes I did not solve it...I'm stuck with this problem for a few days and I ran out of ideas. I understand the concept of your idea but I dont know how to make the loop.

int control = 1;

while( control == 1)
{
   if (<IWantToStopTheLoop>)
   {
       control = 0;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.