hi,
I'm supposed to input a string from a file, strip its punctuation from it , and put its letters into an array. Is there some type of string function that can do that?
Printable View
hi,
I'm supposed to input a string from a file, strip its punctuation from it , and put its letters into an array. Is there some type of string function that can do that?
No. Have you tried anything yet? I'll give you a hint:Quote:
Is there some type of string function that can do that?
-create an array of the same length +1 of the original.
-loop through the string, and if you don't encounter punctuation(use ispunct() or write it by hand - easy), then copy to the second string.
-null terminate the second string.
-copy string 2 into string 1.
Not to my knowledge. Read the string on character at a time into the new string using a loop. i.e.
Code:char string[50];
char output[50];
//read to string here
int a = 0;
for(int i = 0; i < strlen(string); i ++)
{
//you could do this several ways. One is to check for a valid character value:
if ((string[i] >= 97 && string[i] <= 122) || //lowercase a-z
(string[i] >= 65 && string[i] <= 90)) //upercase a-z
{
output[a] = string[i];
a ++;
}
}
OR
//Alternatly you could check in the for loop like this by weeding out specific punctuation instead of only allowing characters
if (string[i] != '.' && string[i] != '!' && string[i] != '?') //etc, etc...
{
output[a] = string[i];
a ++;
}
check out ispunct( int ), found in <ctype.h>. It could be useful...
I fixed it. Cant believe I didn't think of this stuff before!!