Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What a character !!

What a character !!

Scheduled Pinned Locked Moved C#
c++question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IceWater42
    wrote on last edited by
    #1

    I have a string, oldWord, that contains exactly 1 space SOMEWHERE in it. I want to generate 26 copies of that word with the space being converted to a letter, 'a' through 'z'. I tried this: string oldWord; string[] newWord; ... for (int c = 0; c < 26; c++) { newWord[c] = oldWord.Replace(' ', 'a'+c ); } The compiler complains about the parameter, 'a'+c , as being an "invalid char", as you probably expected. I've tried lots of different combinations, to no avail. Would you please tell me how i can code that parameter so i get the expected result? Thank you.

    L A G I 4 Replies Last reply
    0
    • I IceWater42

      I have a string, oldWord, that contains exactly 1 space SOMEWHERE in it. I want to generate 26 copies of that word with the space being converted to a letter, 'a' through 'z'. I tried this: string oldWord; string[] newWord; ... for (int c = 0; c < 26; c++) { newWord[c] = oldWord.Replace(' ', 'a'+c ); } The compiler complains about the parameter, 'a'+c , as being an "invalid char", as you probably expected. I've tried lots of different combinations, to no avail. Would you please tell me how i can code that parameter so i get the expected result? Thank you.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi! Do it like this: newWord[c] = oldWord.Replace(' ', (Char)((int)'a'+c)); It converts the 'a' to a value, add the additional value and convert it back to a character. Marcel Erz

      1 Reply Last reply
      0
      • I IceWater42

        I have a string, oldWord, that contains exactly 1 space SOMEWHERE in it. I want to generate 26 copies of that word with the space being converted to a letter, 'a' through 'z'. I tried this: string oldWord; string[] newWord; ... for (int c = 0; c < 26; c++) { newWord[c] = oldWord.Replace(' ', 'a'+c ); } The compiler complains about the parameter, 'a'+c , as being an "invalid char", as you probably expected. I've tried lots of different combinations, to no avail. Would you please tell me how i can code that parameter so i get the expected result? Thank you.

        A Offline
        A Offline
        Anbuselvan
        wrote on last edited by
        #3

        Hi, You can also try this ... string oldWord = "Hello Welcome"; string newWord = ""; for (int c = 0; c < 26; c++) { int newchar = (int)'a'+c; newWord += oldWord.Replace(' ', (char)newchar ); } System.Diagnostics.Debug.WriteLine(newWord); Result: "HelloaWelcomeHellobWelcomeHellocWelcomeHellodWelcomeHelloeWelcomeHellofWelcomeHellogWelcomeHellohWelcomeHelloiWelcomeHellojWelcomeHellokWelcomeHellolWelcomeHellomWelcomeHellonWelcomeHellooWelcomeHellopWelcomeHelloqWelcomeHellorWelcomeHellosWelcomeHellotWelcomeHellouWelcomeHellovWelcomeHellowWelcomeHelloxWelcomeHelloyWelcomeHellozWelcome" Happy Programming!!! :) Regards, P.Anbuselvan Sr.Software Engineer Hyderabad

        1 Reply Last reply
        0
        • I IceWater42

          I have a string, oldWord, that contains exactly 1 space SOMEWHERE in it. I want to generate 26 copies of that word with the space being converted to a letter, 'a' through 'z'. I tried this: string oldWord; string[] newWord; ... for (int c = 0; c < 26; c++) { newWord[c] = oldWord.Replace(' ', 'a'+c ); } The compiler complains about the parameter, 'a'+c , as being an "invalid char", as you probably expected. I've tried lots of different combinations, to no avail. Would you please tell me how i can code that parameter so i get the expected result? Thank you.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Here is another suggestion: Why not use a character counter: int i = 0; for (char c = 'a'; c <= 'z'; c++) newWord[i++] = oldWord.Replace(' ', c); --- b { font-weight: normal; }

          1 Reply Last reply
          0
          • I IceWater42

            I have a string, oldWord, that contains exactly 1 space SOMEWHERE in it. I want to generate 26 copies of that word with the space being converted to a letter, 'a' through 'z'. I tried this: string oldWord; string[] newWord; ... for (int c = 0; c < 26; c++) { newWord[c] = oldWord.Replace(' ', 'a'+c ); } The compiler complains about the parameter, 'a'+c , as being an "invalid char", as you probably expected. I've tried lots of different combinations, to no avail. Would you please tell me how i can code that parameter so i get the expected result? Thank you.

            I Offline
            I Offline
            IceWater42
            wrote on last edited by
            #5

            Thank you ALL for the excellent suggestions. I tried the 1st 2 suggestions before the 3rd was made ... they both worked great ... PLUS i got the added benefit of learning how to 'cast'. Thanks again,

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups