Alex G
Alex G

Reputation: 21

Concatenating Strings-changing words into pig latin

I keep receiving "Segmentation fault (core dumped)". How can I swap the first letter of a given word that the user inputs to the end of the word and then add an "ay". For example: input "Code" output "odecay"

#include<stdio.h>
#include<string.h>
int main()
{
   char pig[100],p[10],i[100];
   int j,length;
   printf("What word would you like to change into pig latin");
   scanf("%s",pig);
   length=strlen(pig);
   strcat(p,pig[0]);

   for(j=0;j<length;j++)
   {
      pig[j]=pig[j+1];
   }
   strcat(pig,p);
   strcat(pig,"ay");
   printf("%s",pig);
   return 0;
}

Upvotes: 2

Views: 5191

Answers (3)

nalyd88
nalyd88

Reputation: 5138

This code runs but there is a slight problem with your algorithm. Since this is probably homework I'll let you figure that part out.

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

int main()
{
  // These should be initialized before use.
  char pig[100] = "", 
  char p[10] = "";

  printf("What word would you like to change into Pig Latin: ");
  scanf("%s", pig);

  unsigned long length = strlen(pig); // strlen returns an unsigned long
  strcat(p, &pig[0]); // This needs a pointer to char

  for(int j = 0; j < length; j++)
  {
    pig[j] = pig[j + 1];
  }

  strcat(pig, p);
  strcat(pig, "ay");
  printf("%s", pig);
  return 0;
}

Input:

Code

Output:

odeCodeay

As I said, the algorithm is not quite right but now that the code runs you should be able to fix it pretty quick. Also, since you are new to programming notice some of the code formatting which makes it more readable.

EDIT

Since others have already mentioned it, changing the line strcat(p, &pig[0]); to strncat(p, pig, 1); will produce the desired output and still use your original algorithm.

Upvotes: 0

alk
alk

Reputation: 70941

How can I swap the first letter of a given word that the user inputs to the end of the word and then add an "ay"

  1. Save the first character ("letter")

    char c = pig[0];
    
  2. Move the rest of pig one char to the beginning

    memmove(pig, pig + 1, strlen(pig) - 1); 
    

    alternativly use this statement

    memmove(&pig[0], &pig[1], strlen(pig) - 1);
    

    (Note that memcpy() won't work here as source and destiantion overlap.)

  3. Replace the "old" last character with the "old", stored first character

    pig[strlen(pig) - 1] = c;
    
  4. Append "ay"

    strcat(pig, "ay");
    
  5. Print the result:

    printf("%s\n", pig);
    

There is no need for a second "string", char-array.


Assuming pig is large enough, that is one char larger then the data to be scanned in from the user, one can even ommit the use of the intermediate character `c, as per my sketch above.

  1. Initialise pig to all 0s

    char pig[100] = "";
    
  2. Scan in data

    scanf("%98s", pig); /* Add tests for failure reading as needed. */
    
  3. Append the first character of the input, that is copy it to the end of pig

    pig[strlen(pig)] = pig[0];
    
  4. Move all of pig one character to the beginning

    memmove(pig, pig + 1, strlen(pig) - 1);
    
  5. Print the result:

    printf("%s\n", pig);
    

Upvotes: 2

Kaustav Ray
Kaustav Ray

Reputation: 754

strcat(p,pig[0]); // segmentation fault may happen in this line.

char *strcat(char *dest, const char *src) // takes two string but you are passing pig[0] in the second argument which is char

You can use char *strncat(char *dest, const char *src, size_t n)

Thus the proper way to concat a char to a string would be

strncat(p,&pig[0],1); // where 1 is passed in the third argument 

//so that it reads only 1 char i.e. pig[0] and ignore next characters

// otherwise the whole pig string will be concatenated.

Upvotes: -1

Related Questions