CFanatic

Go Back   CFanatic > Programming > C Programming

How to insert a string into string? topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 03-13-2008, 03:30 AM
Junior Member
 
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Tran is on a distinguished road
| More
How to insert a string into string?

I am new programmer in C. I meet a difficult when I write a code " insert a string into a string".

For example:

I have a string: "she is 21 old"
I want to insert "years" between "21" and "old". (position is 10)
Output is : she is 21 years old.

This is my code:


Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	char position, str[100],substr[20];
	int i, count ;
	
	printf("-------------------------\n");
	printf("Enter a sentence (str): ");
	gets(str);
	printf("\n Insert a word (substr): ");
	gets(substr);
	printf("\n Insert word at position (i): ");
	gets(i);
		
	for (count = 0; count < strlen(str)-1; count ++)
	{
		if (count == i)
		{
			strcat(str,substr);
			printf("%s\n", str);
		}
	}
	return 0;
}
when i compile this code, I recieved a warning about gets function. And when i run it, after i enter 2 strings and position. I recieved an error: segmentation fault (core dumped)

Please help me how to fix it, thank so much.

Tran

Last edited by shabbir; 03-13-2008 at 04:28 AM. Reason: Code block
Reply With Quote
  #2  
Old 03-18-2008, 10:03 AM
Junior Member
 
Join Date: Mar 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
heavensgate15 is on a distinguished road
| More
Post Re: How to insert a string into string?

Hope that I'm not late.. hehe, about that "gets" thing, you cannot use it in a variable which the datatype is not a string. Also, an array of characters is different from a string because in a string, there's a sentinel value at the end of the array which is '\0' and it automatically put at the end if the user enters a sentence or a word while in an array of characters, there's no sentinel value at the end of the array. When would a variable be an array of characters? Well, I'm not sure but maybe when you scan the letters one by one.. And about that string, don't worry, it's not your job to put that NULL thing; it's the work of the computer itself...

My point here is that, you cannot use predefined functions if the variable or any value in the function parameter is different from the one declared in that predefined function... To see the different predefined functions in C, press F1.


Hope you understand my lecture....

Last edited by heavensgate15; 03-18-2008 at 10:16 AM. Reason: there's something wrong in that sentence
Reply With Quote
  #3  
Old 05-21-2008, 08:41 AM
Banned
 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
tuvw300 is an unknown quantity at this point
| More
Smile guild

Understood not greatly whether you do send to you more articles I to post the box unit or the short news issue I. I want to understand more

Last edited by shabbir; 05-21-2008 at 09:49 PM. Reason: Confine links to signatures
Reply With Quote
  #4  
Old 05-21-2008, 09:51 PM
shabbir's Avatar
Administrator
 
Join Date: Sep 2006
Posts: 1,225
Thanks: 79
Thanked 71 Times in 48 Posts
shabbir has a spectacular aura aboutshabbir has a spectacular aura about
| More
Re: guild

If you continue to spam the forums your account could be banned and your sites could be flagged as spam.
Reply With Quote
Reply

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multibyte string conversions rajeshgalla C Programming 4 12-07-2010 07:40 AM
Plz help to insert an image to an 3d application developed by using opengl with mfc dathatreya OpenGL Programming 1 12-04-2010 11:20 AM
Where does the string gets stored? Iqbal_h_a C Programming 7 07-31-2008 04:24 PM
string to double kimw C Programming 1 12-26-2006 01:25 AM
rotate string iwdu15 C# Programming 0 09-29-2006 12:48 AM



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.