0
Help!!! Trying to return a string
Char* alphaonly(char str[]){ for(int i = 0;i <= strlen(str);i++) If(!(isalpha(str[i]))); continue; return str; } Can someone help with this, I’m trying to write a function that takes input removes other characters and returns only the alphabet in the main function
2 Antworten
+ 5
Your safest bet is to do it non-destructively, take the input as const char pointer, and create a new string in the function to return. You will be needing knowledge in dynamic memory management for that.
The issue will be that the caller of your function has to know and remember to free the resource allocated through the call. To avoid that,a strategy can be to take in a pointer to a destination array that the caller has to provide and make sure that it is big enough.
Once you have decided
- on which version you want to implement, and
- that you know enough dynamic memory management,
then we can talk about the actual implementation.
+ 1
Thanks for the answer, unfortunately i don’t think ik enough about dynamic memory allocation to handle this, so i’ll just try brush up on that first