1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Help With String Manipulation In C

  1. Jan 23, 2008 #1
    1. The problem statement, all variables and given/known data
    I need to write a function which will replace any occurence of "cmpt" with "XXXX" in a string.

    3. The attempt at a solution
    I am still confused with pointers and string manipulations. Here is what I have so far but i'm lost.

    Last edited by a moderator: Apr 23, 2017
  2. jcsd
  3. Jan 23, 2008 #2


    User Avatar
    Science Advisor
    Homework Helper

    You don't have to use pointers!

    char string[] = "belgiumcmptbelgium";
    for (int i=0;i<strlen(string)-4;i++) {
    if ( 'c'==string && 'm'==string[i+1] && 'p'==string[i+2] && 't'==string[i+3] ) {

    Not exactly elegent - but you get the point!
  4. Jan 24, 2008 #3
    Ok, I figured out my problem and I still was able to use the strstr function cuz our teacher wants us to use one of the string functions. Thanks for the idea, i incorporated it. Sometimes the simplest way is the best! Thanks.
    Last edited: Jan 24, 2008
  5. Jan 25, 2008 #4


    User Avatar
    Science Advisor
    Homework Helper

    I was going to be sarcastic and suggest just use the strstr() function !
    But i assumed this was one of those stupid homework/interview questions where you aren;t allowed to just use the real solution.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook