473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

string combining

11
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. void interpose(char s[], char a[], int pos)
  6. {
  7.     char b[80];
  8.     int j=0;
  9.     int l=0;
  10.     for(int i=pos;s[i]!='\0';i++)
  11.  
  12.     {
  13.         b[j]=s[i];
  14.         j++;
  15.     }
  16.     b[j]='\0';
  17.     for(int c=0;a[c]!='\0';c++)
  18.     {
  19.         s[pos]=a[c];
  20.         pos++;
  21.     }
  22.     for (int k=pos;b[l]!='\0';pos++)
  23.     {
  24.         s[pos]=b[l];
  25.         l++;
  26.     }
  27.     s[pos]='\0';
  28. }
  29.  
  30. int main()
  31. {
  32.     char str1[80];
  33.     char str2[80];
  34.     int posi;
  35.     printf("String1:\n");
  36.     gets(str1);
  37.     printf("String2:\n");
  38.     gets(str2);
  39.     printf("Sayıyı giriniz");
  40.     scanf("%d", &posi);
  41.     interpose(str1,str2,posi);
  42.     puts(str1);
  43.  
  44.  
  45.     return 0;
  46. }
The program should take 2 string parameter and 1 int parameter for combining. (without any str functions, the functions should be written by coder)

my problem is when i enter int value program crashes.
For example,
str1='Good';
str2=' Morning';
int x= 5;
The output will be 'Good Morning'
Mar 20 '12 #1

✓ answered by donbock

If you call interpose() with these arguments:
interpose("Good", "Morning", 5);

then from line 5, s points at "Good" and line 10 is equivalent to
Expand|Select|Wrap|Line Numbers
  1. for (i=5; s[i]!='\0'; i++)
  2.  
  3. s[0] == 'G'
  4. s[1] == 'o'
  5. s[2] == 'o'
  6. s[3] == 'd'
  7. s[4] == '\0'
s[5] is beyond the end of the array, and this access thus causes undefined behavior.

2 1721
donbock
2,426 Expert 2GB
If you call interpose() with these arguments:
interpose("Good", "Morning", 5);

then from line 5, s points at "Good" and line 10 is equivalent to
Expand|Select|Wrap|Line Numbers
  1. for (i=5; s[i]!='\0'; i++)
  2.  
  3. s[0] == 'G'
  4. s[1] == 'o'
  5. s[2] == 'o'
  6. s[3] == 'd'
  7. s[4] == '\0'
s[5] is beyond the end of the array, and this access thus causes undefined behavior.
Mar 22 '12 #2
logantr
11
Done.Thank to you for satisfying answer.
Mar 23 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: John Velman | last post by:
I've used perl for a lot of 'throw away' scripts; I like Python better in principle, from reading about it, but it was always easier to just use perl rather than learn python. Now I'm writing a...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
10
by: ycg0771 | last post by:
I'm trying to modify the following program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the...
2
by: yesh81 | last post by:
Hi , can anybody write a java program to validate IP Addresses using string tokenizer.
3
by: WP | last post by:
Hello! I need some help with my program...it's supposed to read infix expressions line by line from stdin and each expression should be divided into operands and operators and added to a vector of...
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
2
by: John Brawley | last post by:
Please what is the functional C++ difference between a string of characters typed manually and included inside the main() function of a program... vis: string str = "123,4.56,7.8"; , ....and...
3
by: acs7737 | last post by:
i have this hw problem for class thats supposed to read a comma separated list of my cd collection ex.: artist,album artist,album artist,album it reads that into a string and then has to separate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.