![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2008
Posts: 3
Rep Power: 0
![]() |
C char array....need help!
Hello guys.
just wondering if you can help. If i have a C char array: char word = "he?llo?"; Before i print it out, i want to insert a backslash before any question marks. (im working on a regular expressions program so i want to escape the ? metacharacter with a backslash). i want to manipulate it so when print the above out it will look like this: he\?llo\? does anyone know how this can be done? id really appreciate it... thanks a mill ![]() |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Re: C char array....need help!
Escape it:
c Syntax (Toggle Plain Text)
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Re: C char array....need help!
@Narue: I think 'word' here intended to be a user input.
@angel face: since you're not using C++, you can't use the string class. Too bad. What you can do is read the string one char at a time and copy it to another one replacing the ? with /?: c Syntax (Toggle Plain Text)
|
|
|
|
|
|
#4 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Re: C char array....need help!
>@Narue: I think 'word' here intended to be a user input
And your point is? >if(word[i] == '?') newWord[j++] = '\'; The solution is identical, escape a backslash to print a backslash. Your example wouldn't compile because the character isn't properly terminated: c Syntax (Toggle Plain Text)
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
Re: C char array....need help!
OpenLoop, you're missing a semi-colon in your for loop. As Narue pointed out, you don't escape the backslash. And you need to null-terminate the string as well. The following modification will null-terminate, and not be limited to words of length 7.
len = strlen(word);
for(i=0, j=0; i < len; i++) {
if(word[i] == '?') newWord[j++] = '\\';
newWord[j++] = word[i];
}
newWord[j] = '\0';Alternatively, i <= len can be used to implicitely null-terminate.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges! Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download. |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Re: C char array....need help!
you can't hardcode a slash in there. I understand that you provided a way to do it rather than a method, but I was under the impression the OP was looking for the latter (I could be wrong).
@Sane: thanks for the correction. I knew that highlighting didn't look right! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Array Sorting | grimpirate | Software Design and Algorithms | 18 | Jan 24th, 2008 10:04 PM |
| dynamic array help | quickster12 | C++ | 4 | Nov 30th, 2007 12:52 AM |
| problem processing file into a char array | csrocker101 | C++ | 1 | May 9th, 2007 12:50 AM |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 9:19 PM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 3:36 AM |