How could I reduce the length of the code?

Find length of string excluding spaces and repeated char c++ code?? Any help?

  • Hi, Excuse me please, I'm new to programming and to c++ specially , I'm trying to write a code for this problem : Character set of a text is the set of all the characters which are used at least once in the text. you are given an string of at most 200000 characters which contains only ASCII characters except whitespaces. you have to find the length of its character set. Input First line contains T the number of test cases. Each of next T lines contains an string S of at least 1 and at most 200000 characters. Output For each test case print the answer in a new line. Example Input: 2 abA contact@nsit_programming_group Output: 3 14 This is all i could write,, I don't know if i'm not understanding it or not! but, my code is incorrect, and incompleted!! #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int T; string x; cin>>T; for(int i=0;i<T;i++) {getline(cin,x); cout<<x.size(); } system("PAUSE"); return EXIT_SUCCESS; } So , can anyone give me hints for it ,plz? or tell me what should i do to fix it ? or at least give me an algorithm for this code,, Thanks in advance :)

  • Answer:

    The "length of the character set" is not the length of the string but the number of different characters used. You have to look at each character in turn and decide if it has appeared already. For your purposes the quickest way to do that would be to declare an array of 128 integers and initialise them to zero (the ASCII table runs from 0 to 127). For each character, use it as an index into the array. If the value there is 0, the character has not previously been used, and the array value should be then set to 1. At the end, count up the 1s to give a character count.

Nature at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.