Issues with string processing in C

后端 未结 3 980
离开以前
离开以前 2021-01-25 14:02

I\'m stuck on a HW assignment where I\'m required to write a program that transforms a bunch of English words (in a list separated by new lines in a input .txt file) into a bunc

3条回答
  •  深忆病人
    2021-01-25 14:29

    There are a variety of oddities in this code, but the problem you're asking about is created by your while loop in convertToPigLatin. You loop while *strPtr != '\0', and \n is certainly not \0, so you are adding that to pStr.

    Now for the rest of the code, just a few comments:

    • Using strncat to copy one character is odd usage. Typically, one would use simple character assignment instead (as in str1[i] = str2[j])
    • *strPtr++ is incrementing the pointer and dereferencing it, but then doing nothing with the dereferenced value. You just want strPtr++ there.
    • You can create a string literal using char str[] = "some string". You don't need to use array initialization syntax.

    Those are the ones that jumped out at me without a detailed reading. Good luck on your future assignments.

    Edit to add that stepping through your code with a debugger is very valuable in these cases. You would see exactly where the newline is being added.

提交回复
热议问题