Help With String Manipulation In C

In summary, the conversation discusses the task of writing a function to replace occurrences of "cmpt" with "XXXX" in a string. The participant shares their attempt at a solution using pointers and string manipulations, but expresses confusion. Another participant suggests using the strstr() function, which is eventually incorporated into the solution. There is also mention of the challenge of not being allowed to use the most straightforward solution.
  • #1
NDiggity
54
0

Homework Statement


I need to write a function which will replace any occurence 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
  • #2
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!
 
  • #3
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:
  • #4
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.
 

1. What is string manipulation in C?

String manipulation in C refers to the process of modifying or manipulating strings of characters in a C program. This can include tasks such as concatenating strings, searching for specific characters or substrings, and replacing characters within a string.

2. How do I concatenate two strings in C?

To concatenate two strings in C, you can use the strcat() function, which takes in two strings as arguments and combines them into one. For example, strcat(string1, string2) would combine the strings stored in string1 and string2 and store the result in string1.

3. Can I compare two strings in C?

Yes, you can compare two strings in C using the strcmp() function. This function takes in two strings as arguments and returns an integer value based on whether the strings are equal or not. If the strings are equal, it returns 0. If the first string is greater than the second, it returns a positive value. If the first string is less than the second, it returns a negative value.

4. How do I find the length of a string in C?

To find the length of a string in C, you can use the strlen() function, which takes in a string as an argument and returns an integer value representing the length of the string. For example, strlen(string) would return the length of the string stored in the variable string.

5. Can I convert a string to uppercase or lowercase in C?

Yes, you can convert a string to uppercase or lowercase in C using the toupper() and tolower() functions, respectively. These functions take in a character as an argument and return the uppercase or lowercase version of that character. To convert a whole string, you can use a loop to iterate through each character and apply the function to each one.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
935
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top