Thread: String Program

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    11

    Question String Program

    hello Everybody,
    here in this program. we are getting the values for the corresponding characters. eg., VIJAY = 61111 and the total is 10. After that i have to print the array value....( that is similiar to fibonacci series) like., 6 1 1 1 1 = 7 2 2 2 = 9 4 4= 13 8 =4 11 = 5 2 = 7. the ouput shld be a single value. Here i cant get the desired array value......... plz help me


    Code:
    #include<iostream.h>
    #include<conio.h>
    int main()
    {
    enum Days{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
    
    //Days te;
    int sum=0,arr[26];
    char name[20];
    int aa;
    clrscr();
    cout<<"Please enter  a string :";
    cin>>name;
     for(int ia=0;name[ia]!='\0';++ia)
          {
    
    //      cout<<name[ia]<<'\t';
    
          if(name[ia]== 'A' || name[ia] == 'I'|| name[ia] == 'J'|| name[ia] == 'Q'|| name[ia] == 'Y')
          {
           aa= 1;
          }
           else if(name[ia]=='B' || name[ia]=='K' || name[ia]=='R')
           {
    	aa=2;
           }
           else if(name[ia]=='C' ||name[ia]=='G'|| name[ia]=='L'||name[ia]=='S')
           {
    	aa=3;
           }
           else if(name[ia]=='D' ||name[ia]=='M'|| name[ia]=='T')
           {
    	aa=4;
           }
           else if(name[ia]=='E' ||name[ia]=='X'|| name[ia]=='H'||name[ia]=='N')
           {
    	aa=5;
           }
           else if(name[ia]=='U' ||name[ia]=='V'|| name[ia]=='W')
           {
    	aa=6;
           }
           else if(name[ia]=='O' ||name[ia]=='Z')
           {
    	aa=7;
           }
           else if(name[ia]=='F' ||name[ia]=='P')
           {
    	aa=8;
           }
      
    	sum = sum+aa;
    	arr[ia]=aa;
    	cout<<arr[ia]<<" ";
    
    	for(int i=0;arr[i]!='\0';i++)
    	{
    	   arr[ia]=arr[ia]+arr[ia+1];
    
    	   cout<<"\n\t array value : "<<arr[ia]<<endl;
    	}
    
       }
      cout<<"Total sum is:"<<sum;
      getch();
      return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Please post what you understand to be the problem.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    11
    VIBC = 6 1 2 3

    Calculatioc: [ 6+1=7
    1+2=3
    2+3=5 ]

    7 3 5

    calculation: [ 7+3=10
    3+5=8 ]

    108

    calculation: [ 1+0=1
    0+8=8]

    18

    calculation: [ 1+8=9]

    Answer = 9.
    This should the output sir....

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    11
    VIBC = 6 1 2 3

    Calculatioc: [ 6+1=7
    1+2=3
    2+3=5 ]

    7 3 5

    calculation: [ 7+3=10
    3+5=8 ]

    108

    calculation: [ 1+0=1
    0+8=8]

    18

    calculation: [ 1+8=9]

    Answer = 9.
    This should the output sir....

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You'll need to work with strings for the most of your program. For instance,
    Code:
    string aa = "" ; 
    .
    .
    .
    if(name[ia]== 'A' || name[ia] == 'I'|| name[ia] == 'J'|| name[ia] == 'Q'|| name[ia] == 'Y')
          {
           aa += "1" ;
    and so on.

    Then, after you have processed your letters, your next loop will then take the first character in aa, make it an int, and then tale the second character in aa, make it an int, and add the two. Take the result, convert back to a string, and store it in a new string. Continue for each letter in aa.

    That's basically it - I've let out several details.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also, you need to decide if you are writing in C or C++. My example uses a C++ construct.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    11

    Reply

    I have used C++.... Can some help me in this coding.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    int sum=0, arr[26];
    string name;
    int aa, ia, i, remaining;
    cout<<"Please enter  a string :";
    cin>>name;
     for(ia=0; ia<name.size(); ++ia)
     {
          if(name[ia] == 'A' || name[ia] == 'I' || name[ia] == 'J'|| name[ia] == 'Q'|| name[ia] == 'Y')
           aa= 1;
          else if(name[ia]=='B' || name[ia]=='K' || name[ia]=='R')
           aa=2;
          else if(name[ia]=='C' ||name[ia]=='G'|| name[ia]=='L'||name[ia]=='S')
          	aa=3;
          else if(name[ia]=='D' ||name[ia]=='M'|| name[ia]=='T')
           aa=4;
          else if(name[ia]=='E' ||name[ia]=='X'|| name[ia]=='H'||name[ia]=='N')
           aa=5;
          else if(name[ia]=='U' ||name[ia]=='V'|| name[ia]=='W')
          	aa=6;
          else if(name[ia]=='O' ||name[ia]=='Z')
          	aa=7;
          else if(name[ia]=='F' ||name[ia]=='P')
          	aa=8;
          sum += aa;
          arr[ia] = aa;
          cout<< aa <<" ";
       }
       cout << endl;
       arr[ia] = 0; //set one more element as the terminating element
       for(int j=0; j<name.size(); ++j)
        { 
            for(i=0; arr[i+1] != 0; ++i) {
    	cout << arr[i] << "+" << arr[i+1] << "=";
            arr[i] += arr[i+1];
    	cout << arr[i] << endl;
       }
       if (i) arr[i] = 0;
       remaining--;
       }
      cout<<"Total sum is:"<<sum << endl;
      cout<<"Special Value is:"<<arr[0];
      return 0;
    }
    Something like that. I don't know if I have the time to test it. Some bounds in the for-loops might not be correct. What I changed is this:
    1) You read the letters of the string name and save them in the int array arr. First job done. You have also calculated the sum
    2) You do your special calculations. You do total name.size() calculations (not sure, just saw it from your example). Each time you do calculations until the last number (which is set to 0).

    Check out the code, even if it doesn't work/compile and post what you think...

    EDIT:
    Just to point out some errors. Why do you check arr with '\0'? You haven't set any element of arr as '\0' nor have you initialized arr[] to '\0'. So the check won't work
    You read one character, set the value to aa, copy it to arr[ia]. Ok. But then you do a loop that has ia+1, which you haven't read yet. You have to do it in two steps, as in my code. Not with an inner loop.
    You can use char arr, but learn to use string in C++. So name can be string.
    EDIT2:
    Fixed. Almost works.
    EDIT3:
    Didn't pay attention about what happens when the result is >9. Sorry. It seems to need more work...
    Last edited by C_ntua; 09-13-2008 at 12:10 PM.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    arr[ia]
    what if the string is longer than 26 characters?

    also
    #include <string>
    is missing
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, you should add a check for input being more than 26 characters.
    The best solution I can think is to check if the result is > 9. If it is you will have to separate the number with /10 and &#37;10 and store the two digits in arr[i] kai arr[i+1] after you shift the rest of the numbers in arr. You have to check how many times you shift and adjust teh arr[i] = 0 accordingly so it nullifies the last element. And you are done. It is not hard. Just make a separate function int shift(char *array) that shifts the elements of an array and returns the number it shifted.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    I got it in this way ...

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	enum Days{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
        string lineIn;
        vector<int> iVec;
        int  aa;
    
    	cout.width(6);
    	cout << "Please enter  a string :";
    	cin  >> lineIn;
    	cout << lineIn << endl;
    
    	for (int ix = 0; lineIn[ix] != '\0'; ix++) {
    		switch (lineIn[ix]) {
    			case 'A': case 'I': case 'J': case 'Q': case 'Y': aa = 1; break;
    			case 'B': case 'K': case 'R'					: aa = 2; break;
    			case 'C': case 'G': case 'L': case 'S'			: aa = 3; break;
    			case 'D': case 'M': case 'T'					: aa = 4; break;
    			case 'E': case 'X': case 'H': case 'N'			: aa = 5; break;
    			case 'U': case 'V': case 'W'					: aa = 6; break;
    			case 'O': case 'Z'								: aa = 7; break;
    			case 'F': case 'P'								: aa = 8; break;
    			default											: aa = 0;
    		}
    		iVec.push_back (aa);
    		cout << iVec.back () << " ";
    	}
    	cout << endl;
    
    
    	while (iVec.size() > 1) {
    		for (unsigned i = 0; i < iVec.size(); i++) {
    
    			if (i < iVec.size() - 1) {
    				iVec[i] += iVec[i + 1];
    				cout.width(2); cout << iVec[i] - iVec[i + 1] << " + ";
    				cout.width(2); cout << iVec[i + 1] << " = ";
    				cout.width(2); cout << iVec[i] << endl;
    			}
    		}
    		iVec.pop_back();
    	}
    	cout << "Total sum is: " << iVec.front() << endl;
    
    	return 0;
    }

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    lineIn[ix] != '\0'

    who said that c++ string is nul-terminated?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM