My brains have gone dry and flakey - I can't get this to work!

From: Paul F. Johnson (paul_at_all-the-johnsons.co.uk)
Date: 12/06/04


Date: Mon, 06 Dec 2004 14:22:29 +0000

Hi,

Probably the simplest thing in the world, but for some reason, I can't
get it to work. It's basically a search and replace.

Take a string, whizz through it and for each occurence of a letter,
replace it with the capital.

What I have is this (it's a small test)

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string test = "d-mmmm-y", dummy;
   for (int t = 0; t < test.length(); ++t)
   {
      if (test[t] == 'm')
        dummy.insert(t, "M", 0, 1)
      else
        dummy[t] = test[t];
   }
   cout << test << " : " << dummy << endl;
}

This will compile, but aborts with the error

terminate called after throwing an instance of std::out_of_range
   what(): basic_string::insert
Aborted

Okay, I knew insert would be a problem, but the prefered method of

dummy[t] = "M"

gives an error of a conversion from const char * to char *.

As I say, my braincell is befuddled today - any help would be great.

TTFN

Paul



Relevant Pages

  • Re: Implementing strstr
    ... that string doesn't appear at all). ... const char* strstr(const char* haystack, const char* needle) ...
    (comp.programming)
  • curses mouse cut/paste on solaris2.9, hangs on select(). Works fine on aix and linux
    ... The program works fine on solaris, if the user inputs the string from ... const char *defaultText = NULL; ... void tstWprintw; ... int main ...
    (comp.unix.solaris)
  • Pinvoke reading files, returning char * values.
    ... const char* strInputFileName, ... back a string. ... public static extern bool GetXMLString(string inputFileName, ... StringBuilder output, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Whats wrong with the string?
    ... Then it is better to use "const char *" so that the ... >> putc() is a function to put a char on the serial console. ... the str could point beyond the end of your string. ... characters to characters. ...
    (comp.os.linux.embedded)
  • Re: Destroy a string
    ... "rossum" wrote in message ... const char overwriteChar = 'X'; ... The string is still on the heap, ... completely secure, all that is possible is to make the attacker's job ...
    (microsoft.public.dotnet.languages.csharp)