How do I peform this to an array?

This is a discussion on How do I peform this to an array? within the C++ Programming forums, part of the General Programming Boards category; I have an array of characters, for example: "I am zack" And want to substitute a th ("th") in place ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    How do I peform this to an array?

    I have an array of characters, for example:

    "I am zack"

    And want to substitute a th ("th") in place of every "z" in the array. Then I would have to store the result into another array.

    Does anyone know of a way to do this? (I am just starting out)...

    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    One way would be

    Code:
    	char *p = "This is zack";
    	char *result = (char *)malloc(sizeof(char) * strlen(p) * 2); //multiplying by 2 because the worst case scenario would be a string in wich all characters are z
    
    	int index = 0;
    	int occurances = 0;
    
    	for(; p[index] != '\0'; index++)
    	{
    		if(p[index] == 'z')
    		{
    			result[index + occurances] = 't';
    			result[index + occurances + 1] = 'h';
    			occurances++;
    		}
    		else
    		{
    			result[index + occurances] = p[index];
    		}
    	}
    
    
    	result[index+1] = '\0';

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    @ force of will
    since this is in C++, it would be best to use C++ code. that is, using c++ string objects and c++-style dynamic memory.

    and more importantly, its best not to give them the solution.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    The best way I have found to learn coding is by looking at codes and figuring out what they do, I just don't have all the synthax in mind to perform some algorithms, so I appreciate the suggestion.

    However, i don't get some the first few lines of code(malloc...?). Yes, this is c++.

    Also, I forgot to say the user is imputing(cin) the string, so strlen wouldn't really work.

    I do have a way to get the length of the char array the user inputs by converting the array of a certain size (say 256) into a string and then getting the string's length.

    So, at this point, I have the length of string "whatever user inputs".

    with this information, can anyone provide any furthe advice?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Since the suggested solution contains at least one bug, you're probably better off not paying too much attention to it.

    > Also, I forgot to say the user is imputing(cin) the string, so strlen wouldn't really work.
    strlen has nothing to do with input, and strlen will work just fine on strings you input.
    But since you mentioned C++, there are alternatives.

    > with this information, can anyone provide any furthe advice?
    Bugs aside, the posted "solution" is in the right spirit, try using the same approach yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    Quote Originally Posted by nadroj View Post
    @ force of will
    since this is in C++, it would be best to use C++ code. that is, using c++ string objects and c++-style dynamic memory.

    and more importantly, its best not to give them the solution.
    you're totally right, i was also in the C forums and was 2:00 AM my bad.
    Didint use string objects because the Fredir told he was using an array of characters

    Quote Originally Posted by Salem View Post
    Since the suggested solution contains at least one bug, you're probably better off not paying too much attention to it.
    Tell me the bug (I'm also always learning so i would appreciate it :P )

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe this line:
    Code:
    result[index+1] = '\0';
    But I'm not sure - I only spent like 30 seconds looking for a potential bug.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    yap it should be
    Code:
    result[index+ occurances] = '\0';

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    You forgot to count the \0 in your malloc call.

    This is C++, so you should really use new rather than malloc.

    This is C++, so you should really use std::string rather than a char array/pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The low-level algorithm is:
    Code:
    For every character
      if it is a 'z'
        then write "th" to the target
        else write the character to the target
    How you want to implement it depends on your knowledge and your intention. Do you want to learn iteration? Do you want to learn about C++'s algorithms?

    I'd implement it with std::transform(), Boost.Lambda and a std::back_inserter. But that's just me.

    Edit: on second thought, I probably wouldn't do it that way.
    Last edited by CornedBee; 11-07-2007 at 08:08 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Thank you for the input everyone!

    force of will, would you mind explaining what that last line is doing? I tried going one step at a time, but don't get its purpose.

    Thank you once agian.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Ok, here is the code I have right now and it is not working. I would appreciate if anyone can let me know what is wrong with it:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #define MAX_SIZE 200
    
    
    
    int main()
    {
    
    	int index = 0;
    	int times = 0;
    
    	char line[MAX_SIZE];
    
    	cin.getline(line, 200);
    	cout << "You entered: " << line << endl;
    	string str(line);
    	cout << "The size of the line is " << str.size() << " characters.\n";
    
    
    	char result [256];
    
    	for (int i=0; i < str.size(); i++)
    	{
    		if (line [i] == 's')
    		{
    			result[i] = 'z';
    		}
    		
    	    else if (line[i] == 'r')
    		{
    			result[i + times] = 'r';
    			result[i + times + 1] = 'r';
    			times ++;
    		}
    		else 
    		{
    			result[i + times] = line[i];
    		}
    
    	}
    	cout << result << endl;
    
    //	char* dest = new char[newSize + 1];
    
    
    
    
    	return 0;
    }
    Last edited by Fredir; 11-07-2007 at 02:13 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You need to be more specific than "it's not working".

    I think you're having problems because you don't NULL-terminate your result array. Put this just before you print result:
    Code:
    result[i + times] = 0;
    Code:
    		if (line [i] == 's')
    		{
    			result[i] = 'z';
    		}
    		if (line[i] == 'r')
    		{
    			result[i + times] = 'r';
    			result[i + times + 1] = 'r';
    			times ++;
    		}
    
    		else 
    		{
    			result[i + times] = line[i];
    		}
    That second if has to be an else-if; otherwise, when line[i]=='s', the first if statement as well as the else clause will bother be executed.

    You don't have to create an std::string out of your string just to determine how many characters are in the string. strlen() works just fine on char[] strings.

    Code:
    cin.getline(line, 200);
    Why not use MAX_SIZE?
    Code:
    cin.getline(line, MAX_SIZE);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure part of the problem is that you are not setting the end to zero, which leads to printing of "garbage" after the actual string.

    If there is any other problems, please tell us what the symptoms are.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    I am changing s into z and r into rr.

    The code compiles but couts weird symbols.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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