Help With String Manipulation In C

Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving string manipulation in C, specifically the task of replacing occurrences of the substring "cmpt" with "XXXX" within a given string. Participants explore various approaches to solve this problem, including the use of pointers and standard string functions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about pointers and string manipulation, sharing their incomplete attempt at a solution.
  • Another participant suggests a method that does not require pointers, providing a code snippet that replaces the substring directly in the string.
  • A later reply indicates that the original poster resolved their issue by incorporating the strstr function, as per their teacher's instructions, and acknowledges the simplicity of that approach.
  • One participant humorously remarks on the use of strstr, suggesting it might be seen as a straightforward solution that could be frowned upon in a homework context.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to the problem, with differing opinions on the use of pointers versus built-in string functions like strstr. The discussion remains unresolved regarding the preferred method for solving the string manipulation task.

Contextual Notes

Some participants express uncertainty about the requirements of the homework, particularly regarding the use of specific functions or methods. There are also varying interpretations of what constitutes an acceptable solution.

NDiggity
Messages
53
Reaction score
0

Homework Statement


I need to write a function which will replace any occurrence of "cmpt" with "XXXX" in a string.

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.

http://rafb.net/p/2h8fJ210.html
 
Last edited by a moderator:
Physics news on Phys.org
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] ) {
string='X';
string[i+1]='X';
string[i+2]='X';
string[i+4]='X';
break;
}
}

Not exactly elegent - but you get the point!
 
Ok, I figured out my problem and I still was able to use the strstr function because 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:
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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
10
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K