Dividing Character string

This is a discussion on Dividing Character string within the C++ Programming forums, part of the General Programming Boards category; I'm currently working on a simple encryption program. The basic principles work and I've already implemented one method of encryption, ...

  1. #1
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28

    Arrow Dividing Character string

    I'm currently working on a simple encryption program. The basic principles work and I've already implemented one method of encryption, and I want to add another.

    The only problem is, for this, I need to know how to convert a string (inserted by the user of the program) into individual characters, which need to be converted to their respective ASCII characters.

    I have no clue how to go about it... I've been thinking about maybe an array for the results, but I have no idea how arrays work yet, and on top of that, I don't know how to get a specific character in a character string.

    So my question: How can I substract individual characters from a string, and put these into 'a' variable, for later use?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    individual characters, which need to be converted to their respective ASCII characters.
    If you assign a char type to an int variable, the char's ascii code will be stored in the int variable.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    
    int main ()
    {
    	string str = "abcABC";
    	vector<int> v;
    	for(int i = 0; i < str.length(); ++i)
    	{
    
    		v.push_back(str[i]);
    		
    	}
    
    	for(int j=0; j < v.size(); ++j)
    	{
    		cout<<v[j]<<" ";
    	}
    	cout<<endl;
    
    
    	return 0;
    }

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Why not just
    Code:
    #include <iostream>
    #include <string>
    
    
    
    int main ()
    {
    	std::string str = "abcABC";
    	for(int j=0; j < str.length(); ++j)
    	{
    		std::cout<<str[j]<<" ";
    	}
    	std::cout<<std::endl;
    
    
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Because that doesn't meet the op's requirements?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    but I have no idea how arrays work yet, and on top of that, I don't know how to get a specific character in a character string.
    Some people believe you should forget about arrays when first starting with C++ and instead learn about vectors. Yes, I know, "vector" is a scary sounding word, and the syntax can be intimidating, but they are much easier to use than arrays. Here is a short lesson:

    1) Declaring a vector:
    Code:
    #include<iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	vector<int> myVector;
    
    	return 0;
    	
    }
    You declare the type of the things you plan on storing in the vector between the angled brackets. Then, you follow that with a variable name. Don't forget to #include <vector>.

    2) Adding an element to a vector:
    Code:
    myVector.push_back(3);
    That says to add the integer 3 to the end of the vector. A vector will automatically grow in size as you add more elements. That is why vectors are easier to use than arrays. Arrays have a fixed size.

    3) Displaying the contents of a vector:
    Code:
    for(int i=0; i < myVector.size(); ++i)
    {
    	cout<<myVector[i];
    }
    You can get the number of elements in a vector at any time by calling its size() function. To access the elements of a vector, you use brackets with the index position of the element. The elements of a vector start at index position 0. If you write:
    Code:
    myVector.push_back(12);
    myVector.push_back(32);
    myVector.push_back(-5);
    and the vector has no elements in it, then myVector[0] is 12, and myVector[1] is 32, and myVector[2] is -5.
    Last edited by 7stud; 01-18-2007 at 05:41 PM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    "Encryption" -- why do all these ROT-ting kids like to tarnish this divine word with their ROT-ting imitations.

    Also, 7stud, I doubt you will see vectors in use in real encryption applications. Vectors are cool and holy and all that, but that doesn't mean you shouldn't learn arrays at all.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21