Home:ALL Converter>How to add dot character to a character in string?

How to add dot character to a character in string?

Ask Time:2012-11-27T07:34:00         Author:SUE

Json Formatter

I want to add '.' character besides another character in a string but I don't know how to do it ? is it possible?

#include <iostream>

#include <string.h>

using namespace std;

int main(int argc, char *argv[]) {

    string input;
    char dot='.';
    cin>>input;
    for(int i=0;i<input.length();i++)
    {

        if( input[i]>=65 && input[i]<=90)
                {
                    input[i]=input[i]+32;   
                }
        if( (input[i]=='a') || (input[i]=='e') || (input[i]=='i') ||  (input[i]=='o') || input[i]=='y'  || input[i]=='u' )
        {
            input.erase(i,i+1);
        }
        input[i]+=dot;
    }
    cout<<input<<endl;
    return 0;
}

Author:SUE,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/13575010/how-to-add-dot-character-to-a-character-in-string
hinafu :

From the cpluplus.com reference ( http://www.cplusplus.com/reference/string/string/insert/ )\n\n// inserting into a string\n#include <iostream>\n#include <string>\nusing namespace std;\n\nint main ()\n{\n string str=\"to be question\";\n string str2=\"the \";\n string str3=\"or not to be\";\n string::iterator it;\n\n // used in the same order as described above:\n str.insert(6,str2); // to be (the )question\n str.insert(6,str3,3,4); // to be (not )the question\n str.insert(10,\"that is cool\",8); // to be not (that is )the question\n str.insert(10,\"to be \"); // to be not (to be )that is the question\n str.insert(15,1,':'); // to be not to be(:) that is the question\n it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question\n str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)\n str.insert (it+2,str3.begin(),str3.begin()+3); // (or )\n\n cout << str << endl;\n return 0;\n}\n\n\nAlso, check these links:\n\nhttp://www.cplusplus.com/reference/string/string/\nhttp://www.cplusplus.com/reference/string/string/append/\nhttp://www.cplusplus.com/reference/string/string/push_back/",
2012-11-27T00:01:35
James Kanze :

Before you try writing the code, you should write a detailed\nspecification of what it is supposed to do. With your code, I\ncan only guess: convert to lower case (naïvely, pretending that\nyou'll only encounter the 26 unaccented letters in ASCII), then\ndelete all vowels (again, very naïvely, since determining\nwhether something is a vowel or not is non-trivial, even in\nEnglish—consider the y in yet and day), and finally\ninserting a dot after each character. The most obvious way of\ndoing that would be something like: \n\nstd::string results;\nfor ( std::string::const_iterator current = input.begin(),\n end = input.end();\n current != end;\n ++ current ) {\n static std::string const vowels( \"aeiouAEIOU\" );\n if ( std::find( vowels.begin(), vowels.end(), *current )\n != vowels.end() ) {\n results.push_back(\n tolower( static_cast<unsigned char>( *current ) ) );\n }\n results.push_back( '.' );\n}\n\n\nBut again, I'm just guessing as to what you are trying to do.\n\nAnother alternative would be to use std::transform on the\ninitial string to make it all lower case. If you're doing this\nsort of thing regularly, you'll have a ToLower functional\nobject; otherwise, it's probably too much of a bother to write\none just to be able to use std::transform once.",
2012-11-27T00:15:46
Jon Purdy :

I’m assuming you want this input:\n\nHello world!\n\n\nTo give you this output:\n\nh.ll. w.rld!\n\n\nRather than trying to modify the string in place, you can simply produce a new string as you go:\n\n#include <cctype>\n#include <iostream>\n#include <string>\nusing namespace std;\n\nint main(int argc, char *argv[]) {\n string input;\n getline(cin, input);\n string output;\n const string vowels = \"aeiouy\";\n for (int i = 0; i < input.size(); ++i) {\n const char c = tolower(input[i]);\n if (vowels.find(c) != string::npos) {\n output += '.';\n } else {\n output += c;\n }\n }\n cout << output << '\\n';\n return 0;\n}\n\n\nNotes:\n\n\n<cctype> is for toupper().\n<string.h> is deprecated; use <string>.\nRead whole lines with getline(); istream::operator>>() reads words.\nUse tolower(), toupper(), &c. for character transformations. c + 32 doesn’t describe your intent.\nWhen you need comparisons, c >= 'A' && c <= 'Z' will work; you don't need to use ASCII codes.\nUse const for things that will not change.\n",
2012-11-27T00:24:36
David C. Rankin :

I'm not sure how this old question got bumped back onto the current list, but after reviewing the answers, it looks like all will miss the mark if the input is more than a single word. From your comments, it appears you want to remove all vowels and place a '.' before the character immediately prior to where the removal occurred. Thus your example "tour" becomes ".t.r".\nDrawing from the other answers, and shamelessly removing 'y' as from the list of vowels, you can do something similar to:\n#include <iostream>\n#include <string>\n\nint main()\n{\n std::string input;\n if (!getline (std::cin, input)) {\n return 1;\n }\n\n size_t i = 0;\n for (; input[i]; i++)\n {\n switch (input[i])\n {\n case 'A': /* case fall-through intentional */\n case 'E':\n case 'I':\n case 'O':\n case 'U':\n case 'a':\n case 'e':\n case 'i':\n case 'o':\n case 'u':\n {\n size_t pos = input.find_first_not_of("AEIOUaeiou", i+1);\n if (pos == std::string::npos) {\n pos = input.length();\n }\n input.erase(i, pos-i);\n if (pos - i > 1) {\n input.insert(i, 1, '.');\n }\n input.insert(i-1, 1, '.');\n break;\n }\n }\n }\n\n std::cout << input << '\\n';\n}\n\nExample Use/Output\nYour example:\n$ ./bin/vowels-rm-mark\ntour\n.t.r\n\nA longer example:\n$ ./bin/vowels-rm-mark\nMy dog has fleas and my cat has none.\nMy .dg .hs f.l.s. nd my .ct .hs .n.n.\n",
2022-08-04T02:28:36
yy