Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C programming help deleting unwanted character from array?
I have an array of char which is mixture of alphabetic,numeric,symbol values.
for example char[ ]={'a',1,3,4,'B','[','z',}
I need to make a function that will extract alphabetic characters only and store it in other array and return the value of that array
extractAlphabets(char* line){
}
Can anyone help me how to do this.
3 Answers
- JaredLv 71 decade agoFavorite Answer
The above will produce a Seg fault, you cannot return pointers from a function without malloc'ing them.
Is this what you want to do? Return a new array, that's actually kind of advanced. What do you mean return the value of that array?
Here is the problem with arrays. Arrays aren't very useful unless you have both the array AND the length of the array.
Example:
char c[10];
Ok so this creates an array of characters of length 10, but how will you know that it's of length 10, the answer is you DON'T know (unless you memorize that that's how you defined it).
so yes it will work if you always assume that it's length 10.
But you later decide you need 11 chars:
char c[11];
Then you will have to modify your code everywhere...of course you could get around this by doing a #define, but even then you are using prior knowledge of the array.
There are several ways to go about doing this problem depending on what you want to do:
1: create a global array that you are "return"...although if it's global, returning isn't really necessary
2: pass an array as a parameter to be modified...again you don't really need to return return anything here.
3: create an array (malloc) and return this array.
The ONLY way to get around having to know the length of the array is to not really consider it an array at all but rather a string. In C strings are char array's that have a NULL character as their "last" character (it may not actually be the last character) but if you try to print it as a string it will only print the character appearing before the NULL character. Since you seem to think you want to return an array, I'll go with the 3rd way:
#include<stdlib.h>//necessary for malloc
#define BUFF_SIZE 64
char* extractAlphabets(char* line){
char* alphaLine = malloc(BUFF_SIZE * sizeof(char));
char c;
int index = 0;
while((c = *line) != '\0'){//while the current character is NOT the null character
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
alphaLine[index++] = c;
}
++line;
}
alphaLine[index] = '\0';
return alphaLine;
}
I did a couple of things. You might think that I need to keep TWO indexes, one for the new array and one for the previous. However I don't because I can just increment the original array's pointer, don't worry this won't affect the position of the original pointer because the value of the pointer is copied into function so the original pointer will STILL point to the first character.
On the other hand, I DO need to keep an index for the second pointer because if I don't then I will return the last character written into that pointer.
You need to know how long the array should be, so in my case I just used a predefined length of 64 bytes (really 64 chars, but char's are 1 byte long in ANSI C, I think). So if you try to analyze an array longer than 64 characters, then this may produce a seg fault...it pretty much definitely will if there are more than 64 alpha characters in the array.
You can get around this by passing the length of the array to the function:
char* extractAlphabets(char* line, int len){
char* alphaLine = malloc(len * sizeof(char));
...//same from here
}
You may even be unhappy with this because still alphaLine may have more characters than it needs, but there really isn't anything you can do about this. I mean you can add one char at a time (to the memory) and it up with the right amount. This would be efficient memory wise but VERY inefficient time wise because you may have to move the array several times.
ASCII characters are really just 8-bit integers, so the value:
'a' = some number
'z' = some number
'A' = some number
'Z' = some number
So the key exploitation here is that the alpha characters are "in order" that is if 'a' = 61 then 'b' = 62, 'c' = 63, etc. So if the character's value is between 'a' and 'z' then it's a lower case letter and if it's between 'A' and 'Z' then it's an upper case letter. You shouldn't use the actual integer values because that it confusing to read.
Have you learned malloc yet? Because if you haven't learned it yet, then I doubt you should do it this way...on the other hand if you JUST learned malloc then this assignment makes perfect sense.
- Anonymous5 years ago
That is a difficult to answer question, I'll just share my views. Java has been around for say more than 15 years. It is platform independent and device independent. I mean there are no restrictions on the type of device it can go on, anything from huge servers to cell phones. That gives us a huge number of applications running currently on all those systems. So even if Java were to be replaced by some language, it would take another few years to replace each and every app. So the future in question is not near than 10 years. I am saying this as a least possible estimate as I have seen applications which were written in late 80's still running around. It is a mess to maintain such legacy systems, but then no one is really keen on replacing them. Again, I don't make technology so something new might already be on its way, only thing is I haven't seen it yet
- 1 decade ago
Let us think line is the given char array
char* extractAlphabets(char* line){
char* alphaline;
for (int i = 0, j = 0; *(line + i) != "\0"; i ++){
int asciiCode = (int)*(line + i);
if ((asciiCode > 64 && asciiCode < 91) || (asciiCode > 96 && asciiCode < 123)){
*(alphaline + j) = *(line + i);
j ++
}
}
return alphaline;
}
-Ravindra Gullapalli