C++ Insert Chars

Posted:
in Genius Bar edited January 2014
I am making a program and am inputing a whole sentence with [code] getline </pre><hr></blockquote> as a string. I then want to go through and check each character of the string, but i need to find out how to seperate words. How can i detect if its a space and not a letter?



s = the name if my string

numChar = a variable i made from s.length() that is in my for when i check every character but like i said its not working



i was trying:

[code] if(s[numChar]=' ')

{

std::cout&lt;&lt; "space" &lt;&lt; endl;

</pre><hr></blockquote>



but that didn't work, because even when it was a letter it gave me "space"



[ 03-05-2003: Message edited by: ast3r3x ]



[ 03-11-2003: Message edited by: ast3r3x ]</p>

Comments

  • Reply 1 of 14
    stoostoo Posts: 1,490member
    "==" for equality, "=" for assignment. You were assigning each character in s the value '=', which gives true. I'd expect that the complete code would look something like this:



    [code]

    if(s[numChar]==' ')

    {

    std::cout&lt;&lt; "space";

    }

    else

    {

    std::cout&lt;&lt;s[numChar];

    }

    </pre><hr></blockquote>
  • Reply 2 of 14
    ast3r3xast3r3x Posts: 5,012member
    [quote]Originally posted by Stoo:

    <strong>"==" for equality, "=" for assignment. You were assigning each character in s the value '=', which gives true. I'd expect that the complete code would look something like this:



    [code]

    if(s[numChar]==' ')

    {

    std::cout&lt;&lt; "space";

    }

    else

    {

    std::cout&lt;&lt;s[numChar];

    }

    </pre><hr></blockquote></strong><hr></blockquote>



    haha thanks...well at least we know i'm a f*n retard
  • Reply 3 of 14
    ast3r3xast3r3x Posts: 5,012member
    ok my next question (which i just changed the name of this thread) is if i imported it like i said with getline and used a string, if i have the first letter of each word, how would i put the words into alphabetical order? i mean i dont need how to do it all, just asking if there is a way to tell if the character is more important then the other or if i just have to go through and look for all the a's and then all the b's and then all the c's and so on and so forth.
  • Reply 4 of 14
    costiquecostique Posts: 1,084member
    [quote]Originally posted by ast3r3x:

    <strong>ok my next question (which i just changed the name of this thread) is if i imported it like i said with getline and used a string, if i have the first letter of each word, how would i put the words into alphabetical order? i mean i dont need how to do it all, just asking if there is a way to tell if the character is more important then the other or if i just have to go through and look for all the a's and then all the b's and then all the c's and so on and so forth.</strong><hr></blockquote>

    There are many text encodings which complicate matters very much, but in essence the approach is the same. Let's say you work with strings in ASCII encoding. Each letter has its code (A=65, for example). So you can safely compare characters as if they were numbers. Just don't forget about letters' case, because 'A'&lt;'B', but 'B'&lt;'a'.
  • Reply 5 of 14
    mcqmcq Posts: 1,543member
    Yeah... don't forget you may have to look at more than the first character if you had words like "ape", "and", etc. where you have to look at more than the first character. I'm not too familiar with C++, but I figure you could probably make a vector of strings and just use the sort function, I'm not sure.
  • Reply 6 of 14
    ast3r3xast3r3x Posts: 5,012member
    [quote]Originally posted by MCQ:

    <strong>Yeah... don't forget you may have to look at more than the first character if you had words like "ape", "and", etc. where you have to look at more than the first character. I'm not too familiar with C++, but I figure you could probably make a vector of strings and just use the sort function, I'm not sure.</strong><hr></blockquote>



    yeah i thought about looking at multiple letters in a word...man this program i have to make is gonna suck!



    ...guess i have my work cut out for me 2night since its due 2morrow!



    [ 03-05-2003: Message edited by: ast3r3x ]</p>
  • Reply 7 of 14
    mcqmcq Posts: 1,543member
    Ah, The joys of intro programming (and C++). Good luck!
  • Reply 8 of 14
    stoostoo Posts: 1,490member
    This is how I'd sort words, using C. Th major assumption is that C++'s string class supports similar operations to string.h in plain old C:

    <ol type="1">[*]split the source string into words (not sure how you do this efficiently in C++: vectors spring to mind)[*]when you write the comparison function, map each character to the same case before comparing them. i) there may be a string comparison function, ii) there may be a function that maps string to lower case (it's quite simple to write).[*]in C there is a quicksort function, qsort(...). Very handy if the same thing exists in C++ but you're probably expected to write your own sorter.[/list=a]



    Good luck!



    [ 03-05-2003: Message edited by: Stoo ]</p>
  • Reply 9 of 14
    ast3r3xast3r3x Posts: 5,012member
    i can't get the code to insert a character into the end and beginning of a string...can somebody help please?
  • Reply 10 of 14
    thuh freakthuh freak Posts: 2,664member
    [quote]Originally posted by ast3r3x:

    <strong>i can't get the code to insert a character into the end and beginning of a string...can somebody help please?</strong><hr></blockquote>



    here's how i'd do it (probably more complicated than necessary):[code]

    char* str; // old string (before the char to add to beginning and end

    char someChar;//char to add to beginning

    char anotherChar; //char to add to end

    char* dst = (char*)malloc( strlen(str)+3;

    sprintf(dst, "%c%s%c", someChar, str, anotherChar);

    // ...

    //after using dst wherever your gonna use it, make sure to:

    free(dst);

    </pre><hr></blockquote>



    also, i wish i had noticed this thread earlier, cuz theres a test to see if a char is a space (or tab, or new line, or any whitespace). its called 'isspace'. if you include ctype.h you get it (as in the following example) [code]#include &lt;ctype.h&gt;

    ...

    if ( isspace( s+numChar ) ) {

    std::cout &lt;&lt; "space";

    }



    also, theres a way to compare two c-strings. its called 'strcmp'. it returns 0 if they are equal, &lt;0 if the first is less (as in "alpha" compared to "beta"), and &gt;0 if the first is more than the second. that one is case sensitive tho, so you can use 'strcasecmp' to avoid case issues (strcasecmp returns the same thing as strcmp)
  • Reply 11 of 14
    ast3r3xast3r3x Posts: 5,012member
    [code] insert(size_t pos, char *ptr);

    insert(size_t pos, string &str);

    insert(size_t pos, size_t count, char ch);

    insert(iterator it, InputIterator start, InputIterator end);



    Inserts characters at the specified position.



    ----------------------------------



    push_back(char ch);



    Inserts a character at the end of the string.

    </pre><hr></blockquote>



    i wasn't sure how to implement that, could someone give me an example of using that with a string...lets call it InSentence
  • Reply 12 of 14
    thuh freakthuh freak Posts: 2,664member
    [quote]Originally posted by ast3r3x:

    <strong>[code] insert(size_t pos, char *ptr);

    insert(size_t pos, string &str);

    insert(size_t pos, size_t count, char ch);

    insert(iterator it, InputIterator start, InputIterator end);



    Inserts characters at the specified position.



    ----------------------------------



    push_back(char ch);



    Inserts a character at the end of the string.

    </pre><hr></blockquote>



    i wasn't sure how to implement that, could someone give me an example of using that with a string...lets call it InSentence </strong><hr></blockquote>



    basically to insert into the middle of the string, i'd first make a buffer (malloc) thats the size of the old plus the added string (plus an extra byte for the terminating null char). i'd copy the old string into the buffer upto the [pos] position (strncpy). then append on the insert (strcat) to the buffer, and then the remainder of the old string (strcat again). finally, convert the buffer back into a 'string'. i dont really feal like doing too much coding, but the man pages for the parenthetic functions can help churn out the code. the second insert can just convert the 'string' to a c string, and pass it to the first insert. for the third insert(..), i'd make a c string of [count] [ch]'s, and throw it at the first insert. and for the iterator version, i'd try to finagle it similarly (having it pass along eventually to the first insert). then the real work is being done once, and the other functions are just conversions for lazy programmers who dont want to work with c strings. as for the push_back, all you have to do is re-allocate (realloc) the string to strlen + 2 (1 for the null byte, and one for the added char); then strcpy the old into the new, and strncat the new char on.
  • Reply 13 of 14
    ast3r3xast3r3x Posts: 5,012member
    haha...u guys think i'm good enough at programming to get that...i'll just use my crappy method of putting the chars in a vector or char array
  • Reply 14 of 14
    mcqmcq Posts: 1,543member
    I tried writing a small program and it seems to output what you want:



    [code]

    #include &lt;iostream&gt;

    #include &lt;string&gt;



    using namespace std;



    int main()

    {

    \tstring x = "hi";

    \t

    \tcout &lt;&lt; x &lt;&lt; endl;\t// x should be "hi"

    \tx = x + "o";\t\t// adding character to end of string

    \tcout &lt;&lt; x &lt;&lt; endl;\t// x should be "hio"

    \tx = "o" + x;\t\t// adding character to beginning of string

    \tcout &lt;&lt; x &lt;&lt; endl; // x should be "ohio"

    \t

    \t// take substrings - first parameter is start position, second parameter is

    \t// number of characters you want to get

    \tcout &lt;&lt; x.substr(0,2) &lt;&lt; endl;\t// should output "oh"

    \t

    \tstring y = x.substr(1,2) + x.substr(0,1) + x.substr(3,4);

    \t

    \tcout &lt;&lt; y &lt;&lt; endl;\t// should output "hioo"

    }

    </pre><hr></blockquote>



    Heh, I had two years of AP C++ classes, but they taught some sort of twisted or modified C++ STL classes for strings, vector, queues, etc., so I don't actually know much C++. If the above is using C++ STL (I think it is, someone can verify?), then that may be an easier way than messing with char arrays or vectors.



    Edit: substring code



    [ 03-12-2003: Message edited by: MCQ ]</p>
Sign In or Register to comment.