LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2014, 11:38 AM   #1
PeterUK
Member
 
Registered: May 2009
Posts: 281

Rep: Reputation: 16
Problem with tacking a character from a string and replacing it with two.


Hi I may be asking something silly but after searching on online and playing around getting I still getting it wrong.

What I am trying to do is from a string of characters pick a one and replacing with two.

so let said you have char *string_char = "blabla'blabla";

so I have wrote something like:

Code:
char* adding_character3 (char* str)
{
	char *a;
	char *p;
	char temp = 0;
	
	for (a = p = str; *p != '\0'; ++p)
	{	  
	  if (*p == 39){
	     *a++ = 39;
	     *a++ = 39;
	  }else{
	    *a++ = *p;
	  }
	}
    *a = 0;
    return a;
    }
There is something I may be missing in term as knowledge? in the loop the following statement "a = p = str" synchronize all the address every time it increment?

The above code will give a seg fault.

What I want to is a output : "blabla''blabla"

I also try to leave "*a" out of the loop and keep loading and incrementing as it goes alone incrementing in the loop but that didnt work. What is the best way to debug "segmentation faults"?
 
Old 04-03-2014, 11:59 AM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Are you trying to convert a single quote ( ' ) to a double quote ( " )?
It's a different character, not two single quotes.

I haven't compiled this, but it looks right:
Code:
for (p = str;  *p;  ++p)
{	  
    if (*p == '\'')
        *p = '"';
}
 
Old 04-03-2014, 01:38 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by PeterUK View Post
so let said you have char *string_char = "blabla'blabla";
You can't write to a string literal, the compiler puts them in read only pages. Add -Wwrite-strings to get warnings about this. Also, if you actually want to put 2 characters where there was 1 before, you'll need a block of memory that's one byte longer (assuming you're only working with single byte characters).

Quote:
What is the best way to debug "segmentation faults"?
Use a debugger (e.g. gdb). Often, a segfault is caused by earlier code corrupting memory, in that case tools like valgrind can be helpful too. The problem here is pretty straightforward, so there isn't need for that.
 
Old 04-03-2014, 03:37 PM   #4
PeterUK
Member
 
Registered: May 2009
Posts: 281

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by KenJackson View Post
Are you trying to convert a single quote ( ' ) to a double quote ( " )?
It's a different character, not two single quotes.
Sorry the page html messed my post, as you can see 39 is a character, so that is two char where there is one, look down I have found one answer. Thanks
 
Old 04-03-2014, 03:39 PM   #5
PeterUK
Member
 
Registered: May 2009
Posts: 281

Original Poster
Rep: Reputation: 16
[QUOTE=ntubski;5146069]You can't write to a string literal, the compiler puts them in read only pages. Add -Wwrite-strings to get warnings about this. Also, if you actually want to put 2 characters where there was 1 before, you'll need a block of memory that's one byte longer (assuming you're only working with single byte characters).

Thanks this help, I came to that before but that went out of my head :-/. I have found one answer if you would like to give me feedback on the code it would be great. Thanks
 
Old 04-03-2014, 03:44 PM   #6
PeterUK
Member
 
Registered: May 2009
Posts: 281

Original Poster
Rep: Reputation: 16
Here is one way I've found how to do it and as always, I post if I think I got the answer for the threat.

here we go:

Code:
char* adding_character4 (char* str)
{

    char *p;
    int number = 0;
    int c_count_char;
    
    // find how many char " ' " is there
    c_count_char = how_many(str);
    printf ("This is value of how many = %i\n",c_count_char);
    
    number = strlen(str) + 1;
    printf ("This is value of number = %i\n",number);
    
    //adding the new space rfor the extrat char
    number = number + c_count_char;
    
    char buffer [number];
    
    p = str;
    
         int len_buffer;    
     for ( len_buffer = 0; len_buffer <= number; len_buffer++){
       
	  if (*p == 39){
	     buffer [len_buffer] = *p;
	     ++p;
	     len_buffer++;
	     buffer [len_buffer] = 39;
	    
	  }else{
	   buffer [len_buffer] = *p;
	   ++p;
	  }
    
     }
    
    puts (buffer);
    	 
  
     char *text;
     if ((text = malloc(number * sizeof(char))) == NULL) { puts("malloc unable to allocate memory"); exit(1);}
     strcpy(text, buffer);
     printf("This is the value of text =%s\n", text);
    return text;
    }
There is a function called how_many its just return the number of character I am looking for not to work with oversize buffer..etc.

Is there a better way to do this? Thanks

Last edited by PeterUK; 04-03-2014 at 03:46 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Searching/replacing string for string Jalcock501 Programming 13 08-06-2013 02:20 AM
Problem in replacing string in vi editor rajini23 Linux - Desktop 3 02-16-2012 06:33 AM
problem in replacing string in vi editor texasj Linux - Newbie 8 07-26-2011 01:01 AM
the problem in wide character string receiving naveenisback Linux - Software 1 07-07-2009 09:06 PM
Replacing character position within the string of a file scroy Linux - Newbie 13 11-08-2008 05:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration