Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a code i am trying in which string is repalced by its even position character . Let say if string is "abcde" the code should give output "acebd". I haven't implemented the odd position strinng replacement.

What my code do is it stores the even position character continously and when it is done ,it stores "a" to fill the array . I.e output expected by this code is "aceaa" . But it gives segmentation fault at line "str[j]=str[i]" .
Please do help me .


#include<stdio.h>
#include<string.h>

void moveplace(char *str)
{
  int len=strlen(str);
  int i=0,j=0;
  while(str[i]!='\0')
    {
      str[j]=str[i];
      i=i+2;
      j++;
    }

  while(j<len)
    {
      str[j]="a";
      j++;    
}

}

int main()
{
  int i;
  char *str="abcde";
  moveplace(str);
  for(i=0;strlen(str);i++)
    {
 printf("%c ",str[i]);
    }
}
Posted
Updated 9-Sep-13 19:27pm
v3

There are several bugs in your code.
C++
while(str[i]!='\0')
  {
    str[j]=str[i];
    i=i+2;
    j++;
  }

By incrementing i in steps of 2 you will run over the null terminator at the end. And that probably produces you segmentation fault.
C++
while(j<len)>
  {
    str[j]="a";
    j++;
  }

You certainly meant
C++
str[j] = 'a';


[EDIT]
And there is a third one:
C++
for(i=0;strlen(str);i++)

This loop runs forever.
 
Share this answer
 
v2
Comments
Member 10200096 10-Sep-13 3:04am    
that can be corrected using while(str[i]!='\0' && i<len)> but it does not work even then
nv3 10-Sep-13 3:11am    
See the amendment to my solution.
CPallini 10-Sep-13 3:52am    
5.
H.Brydon 10-Sep-13 12:30pm    
+5 back... I didn't realize you posted a solution just before mine. :-)
There may be other problems, but your first loop is defective. You check if the current index location is '\0' (fine) but increment i by 2. So if the NULL is in any odd location (1, 3, 5, 7...), your index will skip over it past the end of the string. The string you pass has 6 characters with the NULL at index 5.
 
Share this answer
 
Comments
nv3 10-Sep-13 3:08am    
+5 - still up that late at night?
H.Brydon 10-Sep-13 12:27pm    
Sadly, yes. I just can't tear myself away from the keyboard. :-(
CPallini 10-Sep-13 3:51am    
5.
The following code should satisfy your requirements:
C
#include<stdio.h>
#include<string.h>

void moveplace(char * str)
{
    int len = strlen(str);
    int lim = len / 2 + len % 2;
    int i;
    for (i=0; i<lim; ++i)
    {
        char k = str[i];
        str[i] = str[i*2];
        str[i*2] = k;
    }
}

int main()
{
  int i;
  char str[]="abcde";
  moveplace(str);
  for(i=0; str[i] != '\0' ;++i)
        printf("%c ",str[i]);
    printf("\n");
}
 
Share this answer
 
Comments
Member 10200096 10-Sep-13 4:16am    
yes it does the same thing as needed :) but you don't have to use extra memory like you did storing characters in char K. And even giving the condition in while loop i
CPallini 10-Sep-13 4:19am    
What's the point? You already use 'extra memory' for 'j' variable, don't you?
H.Brydon 10-Sep-13 12:30pm    
+5. I didn't try it but it looks good.
CPallini 10-Sep-13 12:46pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900