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

27th Oct 2022, 4:50 PM
Wasiu Abass
Wasiu Abass - avatar
2 ответов
+ 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.
27th Oct 2022, 6:10 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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
28th Oct 2022, 12:11 PM
Wasiu Abass
Wasiu Abass - avatar