Thread: Processing outputs of multiple inputs

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    24

    Processing outputs of multiple inputs

    It's not something trivial but I would like to know the best way to process multiple outputs, for example:

    Input
    First line of input will contain a number T = number of test cases. Following lines will contain a string each.

    Output
    For each string, print on a single line, "UNIQUE" - if the characters are all unique, else print "NOT UNIQUE"

    Sample Input
    3
    DELHI
    london
    #include<iostream>

    Sample Output
    UNIQUE
    NOT UNIQUE
    NOT UNIQUE


    So how can I accomplish outputs like that? My code so far is:

    Code:
    int main(int argc, char *argv[]) 
    {
    
         int inputs, count=0;
         char str[100];
         char *ptr;
    
         scanf("%d",&inputs);
    
    
         while(inputs-- >0)
         {
             scanf("%s",str);
    
             for(ptr=str; *ptr!='\0';ptr++)
             {
                 if( *ptr== *(ptr+1))
                 {
                     count++;
                 }
             }
             if(count>0)
             {
                 printf("NOT UNIQUE");
    
             }
             else
             {
                 printf("UNIQUE");
             }
    
         }
    
    }
    But the above will obviously print the output after each input, so I want to know how can I achieve the result given in the problem. Also another thing I want to know is, I am using an array of 100 char, which it can hold a string up to 100 characters, but what do I have to do if I want to handle string with no limit? Just declaring char *str is no good, so what to do?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I suggest you handle each string individually.
    Code:
    for all the strings in the array
    {
       check if str[i] has unique characters
       if yes
         print UNIQUE
       else
         print not UNIQUE
    }
    Of course, there are many ways to do it.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2013
    Location
    Bangalore
    Posts
    5
    Quote Originally Posted by dotnet13 View Post
    It's not something trivial but I would like to know the best way to process multiple outputs, for example:

    Input
    First line of input will contain a number T = number of test cases. Following lines will contain a string each.

    Output
    For each string, print on a single line, "UNIQUE" - if the characters are all unique, else print "NOT UNIQUE"

    Sample Input
    3
    DELHI
    london
    #include<iostream>

    Sample Output
    UNIQUE
    NOT UNIQUE
    NOT UNIQUE


    So how can I accomplish outputs like that? My code so far is:

    Code:
    int main(int argc, char *argv[]) 
    {
    
         int inputs, count=0;
         char str[100];
         char *ptr;
    
         scanf("%d",&inputs);
    
    
         while(inputs-- >0)
         {
             scanf("%s",str);
    
             for(ptr=str; *ptr!='\0';ptr++)
             {
                 if( *ptr== *(ptr+1))
                 {
                     count++;
                 }
             }
             if(count>0)
             {
                 printf("NOT UNIQUE");
    
             }
             else
             {
                 printf("UNIQUE");
             }
    
         }
    
    }
    But the above will obviously print the output after each input, so I want to know how can I achieve the result given in the problem. Also another thing I want to know is, I am using an array of 100 char, which it can hold a string up to 100 characters, but what do I have to do if I want to handle string with no limit? Just declaring char *str is no good, so what to do?
    Take first character in the string then compare with all 2 to n characters. likewise you can check for 2nd with 3 to n and so on. If u find duplicate character in between then print "NOT UNIQUE" and return.
    This may not be the best solution, but it will work. It will take too much time if the string length is very big and there are no duplicate characters or those exist at the end.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Ramabrahmam P View Post
    return.
    There is nothing to return here, except from main.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I suggest you study the problem without help, a good deal more.

    This is an SPOJ, or CodeChef type problem, and really meant for YOU to study and work out. Take the problem without a computer. How would YOU solve this problem? Do that a few times, very slowly, and you'll see patterns being used. Your computer can use those same patterns as the backbone of it's logic.

    Your patterns ------> pseudo code -------> code for your program

    Every resource in computing is finite. The idea of handling an infinite number of char's in a string, or numbers, or anything else, is a completely artificial construct, divorced from reality. You have a string, the string MUST have a string length ( hmmm, strlen() ). You could allocate memory for a longer string, dynamically.
    Last edited by Adak; 11-26-2013 at 09:30 AM.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Location
    Bangalore
    Posts
    5
    return in the sense stop checking that string and take another string as input. No need to check a string if we find a duplicate character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-22-2013, 07:57 AM
  2. Need help in Inputs and outputs
    By keng1135 in forum C Programming
    Replies: 7
    Last Post: 06-08-2012, 02:04 AM
  3. Creating seperate functions for inputs, outputs and calculations?
    By toby2604HASSELB in forum C Programming
    Replies: 3
    Last Post: 10-21-2010, 02:26 AM
  4. segmentation: function called with multiple outputs?
    By bertazoid in forum C Programming
    Replies: 11
    Last Post: 10-27-2008, 12:44 PM
  5. Binary Inputs and Outputs *Files Questions
    By nicz888 in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 03:17 AM