Thread: How do I peform this to an array?

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's a sure sign of a string without a NULL terminator. Add one in, see what happens.
    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.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    I see...
    exactly where should I include
    Code:
    result[i + times] = 0;
    I tried putting into the loop, but it was causing the conversion part not to work at all.

    If i put it outside, it just says i is not identified.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from the several points that dwks pointed out, this will not work right:
    Code:
    		if (line [i] == 's')
    		{
    			result[i] = 'z';
    		}
    Where are you writing the z? Is that REALLY where you want to write it, assuming your input is "resolved result"? Step through it manually and see what happens [or with a debugger if you know how to use one].

    --
    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.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Fredir View Post
    I see...
    exactly where should I include
    Code:
    result[i + times] = 0;
    I tried putting into the loop, but it was causing the conversion part not to work at all.

    If i put it outside, it just says i is not identified.
    Move the "int i" to outside the for-loop and it will work better.

    --
    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.

  5. #20
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    		if (line [i] == 's')
    		{
    			result[i + times] = 'z';
    		}
    		else if (line[i] == 'r')
    		{
                   ...
    These are one thing. You'll also need to null-terminate the result and make sure it is as large as needed.

    Of cource, everything would be easier if you used C++ strings all the way:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string source;
        std::getline(std::cin, source);
        std::string dest;
        for (std::size_t i = 0; i != source.size(); ++i) {
            switch (source[i]) {
                case 's': dest += 'z'; break;
                case 'r': dest += "rr"; break;
                default: dest += source[i];
            }
        }
        std::cout << source << '\n' << dest << '\n';
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Oh the light!

    You all have been extremely helpful, thanks for all those inputs.

    I did what you suggested, and it seems to be working.

    here is the updated version:

    Code:
    // C-String Lab.cpp : Defines the entry point for the console application.
    //
    
    #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];
    	int i;
    	for (i=0; i < str.size(); i++)
    	{
    		if (line [i] == 's')
    		{
    			result[i + times] = 'z';
    		}
    		
    	    else if (line[i] == 'r')
    		{
    			result[i + times] = 'r';
    			result[i + times + 1] = 'r';
    			times ++;
    		}
    		else 
    		{
    			result[i + times] = line[i];
    		}
    	
    		
    	}
    	result[i + times] = 0;
    	cout << result << endl;
    
    //	char* dest = new char[newSize + 1];
    
    
    
    
    	return 0;
    }
    the next thing I have to do is to replace every "th" in the string with a "z". Would I be able to do this just by include another if stamement or should I create another for loop with result since I will be shifting indexes again?

  7. #22
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    One final question:

    for the array result,

    I tried making it

    char result [str. length() * 2] so it would be dynamically allocated. But i got an error saying size of result cannot be 0. I am assuming this is happening because I only get an actual length once the user inputs it. But, how could I accomplish something of that effect?

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could do it in that loop. It wouldn't be too difficult. Something like this:
    Code:
    if(array[index] == 't' && array[index+1] == 'h') {
        result[rindex] = 'z';
    }
    Also note what I said above about not needing to create an entirely new std::string. You could do this instead:
    Code:
    	int i, len = strlen(line);
    	for (i=0; i < len; i++)
    You'd have to include <cstring>, which is where strlen() is located. And you could drop <string>, as yuo wouldn't be using any std::strings.

    [edit]
    I tried making it

    char result [str. length() * 2] so it would be dynamically allocated. But i got an error saying size of result cannot be 0. I am assuming this is happening because I only get an actual length once the user inputs it. But, how could I accomplish something of that effect?
    I don't think you can declare variable-sized arrays in standard C++. Try this instead:
    Code:
    char *result = new char[str.length() * 2];
    
    // ...
    
    delete [] result;
    I'd use str.length()*2+1 for the size, though, because that's the maximum it could be. Don't forget the NULL. [/edit]
    Last edited by dwks; 11-07-2007 at 03:49 PM.
    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.

  9. #24
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Code:
    for (k =0; k < size of result; i++)
    {
    if (result[k] == 't' && result[k+1] == 'h')
     newResult[k] == 'z';
    }

    --> does this seem to be in the right track?

    [EDIT] thanks dwks, I guess I had the idea...let me incorporate those things in...
    Last edited by Fredir; 11-07-2007 at 03:48 PM.

  10. #25
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    I don't think the if statement is working... I will see if I can do the above loop later:

    Code:
    // C-String Lab.cpp : Defines the entry point for the console application.
    //
    
    #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];
    	int i;
    	
    	for (i=0; i < str.size(); i++)
    	{
    		if (line [i] == 's')
    		{
    			result[i + times] = 'z';
    		}
    		
    	    else if (line[i] == 'r')
    		{
    			result[i + times] = 'r';
    			result[i + times + 1] = 'r';
    			times ++;
    		}
    		if (result[i] == 't' && result[i+1] == 'h')
    		{
    			result[i] == 'z';
    		}
    		else 
    		{
    			result[i + times] = line[i];
    		}
    		
    		
    	}
    	result[i + times] = 0;
    	cout << result << endl;
    
    //	char* dest = new char[newSize + 1];
    
    
    
    
    	return 0;
    }

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    else if (line[i] == 'r')
    ...
    if (result[i] == 't' && result[i+1] == 'h')
    See anything different.

    --
    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.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code doesn't take unicode into mind either. It's good practice to try to use TCHAR functions and _T() macro to make it both unicode/ansi safe. It takes some getting used to, I've found out.

  13. #28
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Ok, looks like I completed the code. Here is the final version:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    #define MAX_SIZE 200
    
    
    
    int main()
    {
    
    	int index = 0;
    	int times = 0;
    
    	char line[MAX_SIZE];
    
    	cin.getline(line, MAX_SIZE);
    	cout << "You entered: " << line << endl;
    	cout << "The size of the line is " << strlen(line) << " characters.\n";
    
    	int len = strlen(line);
    	char* result = new char[len*2 +1];
    	int i;
    
    	for (i=0; i < len; i++)
    	{
    		if (line [i] == 's')
    		{
    			result[i + times] = 'z';
    		}
    		else if (line[i] == 'r')
    		{
    			result[i + times] = 'r';
    			result[i + times + 1] = 'r';
    			times ++;
    		}
    		else if (line[i] == 't' && line[i+1] == 'h')
    		{
    			result[i + times] = 'z';
    		}
    		else 
    		{
    			result[i + times] = line[i];
    		}
    
    
    	}
    
    	result[i + times] = 0;
    	cout << "Translation: " << result << endl;
    	delete [] result;
    
    
    	return 0;
    }

    Thank you all for the help. If anyone wants to give tips as how to make the code more efficient for my own benefit in the future, that would be nice.

  14. #29
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your code is pretty good now. One of my only suggestions is this:
    Code:
    	cout << "The size of the line is " << strlen(line) << " characters.\n";
    
    	int len = strlen(line);
    Swap those lines, and you can use the variable len in the cout. strlen() does take a little while to execute, so your program will be marginally more efficient if you do as I suggest.

    Quote Originally Posted by Elysia View Post
    The code doesn't take unicode into mind either. It's good practice to try to use TCHAR functions and _T() macro to make it both unicode/ansi safe. It takes some getting used to, I've found out.
    Both of those are Windows-only functions. You shouldn't recommend them in a program that is otherwise perfectly standard C++. On the other hand, because the OP is including "stdafx.h", they're clearly using a Windows compiler (probably MSVC). Still, when you recommend things like that, at least mention in passing that they're Windows-specific.
    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.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    Both of those are Windows-only functions. You shouldn't recommend them in a program that is otherwise perfectly standard C++. On the other hand, because the OP is including "stdafx.h", they're clearly using a Windows compiler (probably MSVC). Still, when you recommend things like that, at least mention in passing that they're Windows-specific.
    Unicode Windows-specific? Last I looked, using TCHAR resolved to either CHAR or WCHAR_T and ANSI/UNICODE C/C++ standard functions (as well as Windows-only API).

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