Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2008, 6:42 PM   #1
angel face
Newbie
 
Join Date: Feb 2008
Posts: 3
Rep Power: 0 angel face is on a distinguished road
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
angel face is offline   Reply With Quote
Old Feb 28th, 2008, 7:21 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
Re: C char array....need help!

Escape it:
c Syntax (Toggle Plain Text)
  1. char word = "he\\?llo\\?";
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Feb 28th, 2008, 7:29 PM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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)
  1. char word[] = "he?llo?";
  2. char newWord[100];
  3. int i = 0, j = 0;
  4. for(i = 0; i < 7 i++)
  5. {
  6. if(word[i] == '?') newWord[j++] = '\';
  7. newWord[j++] = word[i];
  8. }
  9.  
OpenLoop is offline   Reply With Quote
Old Feb 28th, 2008, 7:55 PM   #4
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
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)
  1. if(word[i] == '?') newWord[j++] = '\\';
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Feb 28th, 2008, 8:38 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,035
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Feb 29th, 2008, 12:36 AM   #6
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: C char array....need help!

Quote:
Originally Posted by Narue View Post
And your point is?
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!
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC