Removing Punctuations in a string in C - c

I'm working on removing punctuations that can exist either beginning or end of a string or could be both.
ex. "!!Hello**"
I want an output of: "Hello" with all of the punctuations removed.
char s[] = "!!Hello**";
ch [] = NULL;
int i = 0;
for (i = 0; i < length; i++) {
if ( isalpha(s[i]) ) {
ch[i]=s[i];
} else {
continue;
}
ch[i] = '\0';
}
The block of code does not seem to copy the strings to ch. No idea why!!

You could change it inplace:
#include <ctype.h>
#include <stdio.h>
int main() {
char s[] = "!!Hello**";
size_t pos = 0;
for (char *p = s; *p; ++p)
if (isalpha(*p))
s[pos++] = *p;
s[pos] = '\0';
printf("'%s'\n", s);
}
Output
'Hello'
Or using only pointers:
#include <ctype.h>
#include <stdio.h>
void filter_alpha(char *s) {
for (char *p = s; *p; ++p)
if (isalpha(*p))
*s++ = *p;
*s = '\0';
}
int main() {
char s[] = "!!Hello**";
filter_alpha(s);
printf("'%s'\n", s);
}
To remove only leading/trailing non-alpha characters
#include <assert.h>
#include <ctype.h> // isalpha()
#include <stdio.h>
#include <string.h> // strlen()
char* strip_nonalpha_inplace(char *s) {
for ( ; *s && !isalpha(*s); ++s)
; // skip leading non-alpha chars
if (*s == '\0')
return s; // there are no alpha characters
assert(isalpha(*s));
char *tail = s + strlen(s);
for ( ; !isalpha(*tail); --tail)
; // skip trailing non-alpha chars
assert(isalpha(*tail));
*++tail = '\0'; // truncate after the last alpha
return s;
}
int main() {
char s[] = "!!Hello**";
printf("'%s'\n", strip_nonalpha_inplace(s));
}

You sort of have the right idea, but you've missed some of the essential things when dealing with strings in C (like strlen)
This should remove any leading and trailing non-alphanumeric ASCII characters.
#include <string.h>
char * remove_outer_punctuation( char * text )
{
int i = 0;
size_t len = 0;
char * start = text;
if ( text == NULL ) return NULL;
len = strlen(text);
if ( len < 1 ) return start;
// advance start to the first alphanum character
for ( i = 0 ; i < len; i++ ){
if ( !isalpha(text[i]) ) {
start++;
} else {
continue;
}
}
// find the final alphanumeric character and
// put a NUL after it
for ( i = len; i > 0; i-- ){
if ( isalpha(text[i] ) continue;
}
text[i+1] = 0x0;
return start;
}
Be warned though, this will modify the input string ( we insert NUL ). If you don't want it do do that, use strcpy first.

Your code has a few issues:
ch [] = NULL;
this should have a type in it's definition:
char ch[15];
Don't define char arrays of undefined size.
Also, what is length in your code? You could use strlen(s) instead of length for which I see no definition.
For tokenization, you can use strtok.
To copy the strings from one array to another, use strcpy_s.

To remove initial stuff:
char * rm_head(char *str) {
while(!isalpha(*str)) str++;
return str;
}
and use it like:
char *rest = rm_head(original);

Related

Why my returned value of strchr is ignored?

I have to make a function, that will code my sentence like this: I want to code all words with an o, so for example I love ice cream becomes I **** ice cream.
But my function ignores the result of strchr. And I don't know why.
This is my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define LEN 1000
char *Shift(char *str, char *let) {
const char *limits = " ,-;+.";
char copy[LEN];
strcpy(copy, str);
char *p;
char *ptr;
ptr = strtok(copy, limits);
for (int j = 0; ptr != NULL; ptr = strtok(NULL, limits), ++j) {
int len = 0;
if (strchr(ptr, let) != NULL) {
p = strstr(str, ptr);
for (int i = 0; i < strlen(ptr); i++) {
p[i] = "*";
}
}
}
return str;
}
int main() {
char *s = Shift("I love my cocktail", "o");
puts(s);
}
Expected output is: I **** my ********
but I've got just printed the original string
For starters the function strchr is declared like
char *strchr(const char *s, int c);
that is its second parameter has the type int and the expected argument must represent a character. While you are calling the function passing an object of the type char * that results in undefined behavior
if (strchr(ptr, let) != NULL) {
It seems you mean
if (strchr(ptr, *let) != NULL) {
Also you may not change a string literal. Any attempt to change a string literal results in undefined behavior and this code snippet
p = strstr(str, ptr);
for (int i = 0; i < strlen(ptr); i++) {
p[i] = "*";
}
tries to change the string literal passed to the function
char *s = Shift("I love my cocktail", "o");
And moreover in this statement
p[i] = "*";
you are trying to assign a pointer of the type char * to a character. At least you should write
p[i] = '*';
If you want to change an original string you need to store it in an array and pass to the function the array instead of a string literal. For example
char s[] = "I love my cocktail";
puts( Shift( s, "o" ) );
Pay attention to that there is no great sense to declare the second parameter as having the type char *. Declare its type as char.
Also the function name Shift is confusing. You could name it for example like Hide or something else.
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
char * Hide( char *s, char c )
{
const char *delim = " ,-;+.";
for ( char *p = s += strspn( s, delim ); *p; p += strspn( p, delim ) )
{
char *q = p;
p += strcspn( p, delim );
char *tmp = q;
while ( tmp != p && *tmp != c ) ++tmp;
if ( tmp != p )
{
for ( ; q != p; ++q ) *q = '*';
}
}
return s;
}
int main( void )
{
char s[] = "I love my cocktail";
puts(s);
puts( Hide( s, 'o' ) );
}
The program output is
I love my cocktail
I **** my ********
The for loop
for ( ; q != p; ++q ) *q = '*';
within the function can be rewritten as a call of memset
memset( q, '*', p - q );
There are multiple problems:
copying the string to a fixed length local array char copy[LEN] will cause undefined behavior if the string is longer than LEN-1. You should allocate memory from the heap instead.
you work on a copy of the string to use strtok to split the words, but you do not use the correct method to identify the parts of the original string to patch.
you should pass a character to strchr(), not a string.
patching the string with p[i] = "*" does not work: the address of the string literal "*" is converted to a char and stored into p[i]... this conversion is meaningless: you should use p[i] = '*' instead.
attempting to modify a string literal has undefined behavior anyway. You should define a modifiable array in main and pass the to Shift.
Here is a corrected version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *Shift(char *str, char letter) {
const char *limits = " ,-;+.";
char *copy = strdup(str);
char *ptr = strtok(copy, limits);
while (ptr != NULL) {
if (strchr(ptr, letter)) {
while (*ptr != '\0') {
str[ptr - copy] = '*';
ptr++;
}
}
ptr = strtok(NULL, limits);
}
free(copy);
return str;
}
int main() {
char s[] = "I love my cocktail";
puts(Shift(s, 'o'));
return 0;
}
The above code still has undefined behavior if the memory cannot be allocated. Here is a modified version that operates in place to avoid this problem:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
char *Shift(char *str, char letter) {
char *ptr = str;
while ((ptr = strchr(ptr, letter)) != NULL) {
char *p = ptr;
while (p > str && isalpha((unsigned char)p[-1]))
*--p = '*';
while (isalpha((unsigned char)*ptr)
*ptr++ = '*';
}
return str;
}
int main() {
char s[] = "I love my cocktail";
puts(Shift(s, 'o'));
return 0;
}
Note that you can also search for multiple characters at a time use strcspn():
#include <ctype.h>
#include <stdio.h>
#include <string.h>
char *Shift(char *str, const char *letters) {
char *ptr = str;
while (*(ptr += strcspn(ptr, letters)) != '\0') {
char *p = str;
while (p > str && isalpha((unsigned char)p[-1]))
*--p = '*';
while (isalpha((unsigned char)*ptr)
*ptr++ = '*';
}
return str;
}
int main() {
char s[] = "I love my Xtabentun cocktail";
puts(Shift(s, "oOxX"));
return 0;
}

how to remove special character from the beginning and end of a string in c

Lets say I have a string like $$$hello$$$ how can I remove $$$ from the beginning and the end to make it look like hello.
another example is $$he$$o$$ after would be he$$o
Here is my alternate solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int main( void )
{
char source_string[] = "$$he$$o$$";
char *p1, *p2;
// find start of substring to extract
p1 = source_string;
while ( *p1 == '$' ) p1++;
//find start of "trash" at end
p2 = &source_string[ strlen(source_string) ];
while ( p2 != p1 && *(p2-1) == '$' ) p2--;
//calculate length of substring to extract
size_t len = p2 - p1;
//allocate space for dest_string including null terminating character
char *dest_string = malloc( len + 1 );
assert( dest_string != NULL );
//extract substring
memcpy( dest_string, p1, len );
//add null terminating character
dest_string[len] = '\0';
//print the result
puts( dest_string );
//free the allocated buffer
free( dest_string );
}
The solution above copies the substring to another buffer. If you want to copy the substring to the start of the source string instead, you can use the function memmove instead of memcpy. It is necessary to use this function instead, because memcpy does not allow the source and destination buffers to overlap, whereas memmove does.
Using this technique to copy the substring to the start of the source string, I propose the following solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int main( void )
{
char str[] = "$$he$$o$$";
char *p1, *p2;
// find start of substring to extract
p1 = str;
while ( *p1 == '$' ) p1++;
//find start of "trash" at end
p2 = &str[ strlen(str) ];
while ( p2 != p1 && *(p2-1) == '$' ) p2--;
//calculate length of substring to extract
size_t len = p2 - p1;
//copy substring to start of string
memmove( str, p1, len );
//add null terminating character
str[len] = '\0';
//print the result
puts( str );
}
Remember first occurance of a letter, this is beg pointer
Place end pointer after every letter
Print result from beg to end
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char input_str[] = "$$$h&e$$l&&lo$$$";
char* beg = NULL, *end = NULL;
int i;
for (i = 0; i < sizeof(input_str); ++i) {
if (input_str[i] >= 'a' && input_str[i] <= 'z') {
end = &input_str[i+1];
if (beg == NULL)
beg = &input_str[i];
}
}
if (!beg || !end)
return 0;
*end = 0;
printf("%s\n", beg);
}
You could also allocate memory by copying the contents of the old string you want into a new string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *trim_word(char *word, char *to_remove)
{
int beg_pos = 0;
int end_pos = strlen(word);
// at the end of the while loop, beg_pos represents the first position
// in the string that is not the char to remove
while (beg_pos < end_pos && word[beg_pos] == to_remove[0])
beg_pos++;
// at the end of the while loop, end_pos is the last position in string
// that is not the char to remove
while (end_pos - 1 > 0 && word[end_pos - 1] == to_remove[0])
end_pos--;
char *new_word = NULL;
// allocate end_pos - beg_pos + 1
new_word = malloc(end_pos - beg_pos + 1);
if (new_word == NULL) {
fprintf(stderr, "Error malloc.\n");
exit(1);
}
// copy in new_word starting at word[beg_pos] for (end_pos - beg_pos) characters
strncpy(new_word, &word[beg_pos], end_pos - beg_pos);
new_word[end_pos - beg_pos] = '\0';
return new_word;
}
int main(void)
{
char *str = "$$$he$$o$$$";
char to_remove = '$';
char *trimmed_word = NULL;
trimmed_word = trim_word(str, &to_remove);
printf("old word: %s\t trimmed word: %s\n", str, trimmed_word);
// free allocated memory in trimmed_word function
free(trimmed_word);
return 0;
}

My own strcat function with pointers does not work right

I am new to pointers and want to learn them well. So this is my own attempt to write my strcat function. If I return just a it prints some binary things (I think it should print the solution), If I return *a it says seg fault core dumped I couldn't find the error. Any help is accepted thanks.
#include <stdio.h>
#include <string.h>
int main() {
char *strcaT();
char *a = "first";
char *b = "second";
printf("%s", strcaT(a, b));
return 0;
}
char *strcaT(char *t, char *s) {
char buffer[strlen(t) + strlen(s) - 1];
char *a = &buffer[0];
for (int i = 0; i < strlen(s) + strlen(t); i++, t++) {
if (*t == '\n') {
for (int i = 0; i < strlen(s);i++) {
buffer[strlen(t) + i] = *(s + i);
}
}
buffer[i] = *(t + i);
}
return a;
}
The code has multiple cases of undefined behavior:
you return the address of a local array in strcaT with automatic storage, which means this array can no longer be used once it goes out of scope, ie: when you return from the function.
the buffer size is too small, it should be the sum of the lengths plus 1 for the null terminator. You write beyond the end of this local array, potentially overwriting some important information such as the caller's framce pointer or the return address. This undefined behavior has a high chance of causing a segmentation fault.
you copy strlen(t)+strlen(s) bytes from the first string, accessing beyond the end of t.
It is unclear why you test for '\n' and copy the second string at the position of the newline in the first string. Strings do not end with a newline, they may contain a newline but and at a null terminator (byte value '\0' or simply 0). Strings read by fgets() may have a trailing newline just before the null terminator, but not all strings do. In your loop, the effect of copying the second string is immediately cancelled as you continue copying the bytes from the first string, even beyond its null terminator. You should perform these loops separately, first copying from t, then from s, regardless of whether either string contains newlines.
Also note that it is very bad style to declare strcaT() locally in main(), without even a proper prototype. Declare this function before the main function with its argument list.
Here is a modified version that allocates the concatenated string:
#include <stdio.h>
#include <stdlib.h>
char *strcaT(const char *s1, const char *s2);
int main() {
const char *a = "first";
const char *b = "second";
char *s = strcaT(a, b);
if (s) {
printf("%s\n", s);
free(s);
}
return 0;
}
char *strcaT(const char *t, const char *s) {
char *dest = malloc(strlen(t) + strlen(s) + 1);
if (dest) {
char *p = dest;
/* copy the first string */
while (*t) {
*p++ = *t++;
}
/* copy the second string at the end */
while (*s) {
*p++ = *s++;
}
*p = '\0'; /* set the null terminator */
}
return dest;
}
Note however that this is not what the strcat function does: it copies the second string at the end of the first string, so there must be enough space after the end of the first string in its array for the second string to fit including the null terminator. The definitions for a and b in main() would be inappropriate for these semantics, you must make a an array, large enough to accommodate both strings.
Here is a modified version with this approach:
#include <stdio.h>
char *strcaT(char *s1, const char *s2);
int main() {
char a[12] = "first";
const char *b = "second";
printf("%s\n", strcaT(a, b));
return 0;
}
char *strcaT(char *t, const char *s) {
char *p = t;
/* find the end of the first string */
while (*p) {
*p++;
}
/* copy the second string at the end */
while (*s) {
*p++ = *s++;
}
*p = '\0'; /* set the null terminator */
return t;
}
It is a very bad idea to return some local variable, it will be cleared after the function finishes its operation. The following function should work.
char* strcaT(char *t, char *s)
{
char *res = (char*) malloc(sizeof(char) * (strlen(t) + strlen(s) + 1));
int count = 0;
for (int i = 0; t[i] != '\0'; i++, count++)
res[count] = t[i];
for (int i = 0; s[i] != '\0'; i++, count++)
res[count] = s[i];
res[count] = '\0';
return res;
}
In the main function
char *strcaT();
It should be declared before main function:
char *strcaT(char *t, char *s);
int main() {...}
You returns the local array buffer[], it's is undefined behavior, because out of strcaT function, it maybe does not exist. You should use the pointer then allocate for it.
The size of your buffer should be +1 not -1 as you did in your code.
char *strcaT(char *t, char *s) {
char *a = malloc(strlen(t) + strlen(s) + 1);
if (!a) {
return NULL;
}
int i;
for(i = 0; t[i] != '\0'; i++) {
a[i] = t[i];
}
for(int j = 0; s[j] != '\0'; j++,i++) {
a[i] = s[j];
}
a[i] = '\0';
return a;
}
The complete code for test:
#include <stdio.h>
#include <stdlib.h>
char *strcaT(char *t, char *s);
int main() {
char *a = "first";
char *b = "second";
char *str = strcaT(a, b);
if (str != NULL) {
printf("%s\n", str);
free(str); // Never forget freeing the pointer to avoid the memory leak
}
return 0;
}
char *strcaT(char *t, char *s) {
char *a = malloc(strlen(t) + strlen(s) + 1);
if (!a) {
return NULL;
}
int i;
for(i = 0; t[i] != '\0'; i++) {
a[i] = t[i];
}
for(int j = 0; s[j] != '\0'; j++,i++) {
a[i] = s[j];
}
a[i] = '\0';
return a;
}
For starters the function strcaT should append the string specified by the second parameter to the end of the string specified by the first parameter. So the first parameter should point to a character array large enough to store the appended string.
Your function is incorrect because at least it returns a (invalid) pointer to a local variable length character array that will not be alive after exiting the function and moreover the array has a less size than it is required to store two strings that is instead of
char buffer[strlen(t) + strlen(s) - 1];
^^^
it should be declared at least like
char buffer[strlen(t) + strlen(s) + 1];
^^^
and could be declared as static
static char buffer[strlen(t) + strlen(s) + 1];
Also the nested loops do not make sense.
Pay attention that you should provide the function prototype before calling the function. In this case the compiler will be able to check passed arguments to the function. And the name of the function strcaT is confusing. At least the function can be named like strCat.
The function can be defined the following way
#include <stdio.h>
#include <string.h>
char * strCat( char *s1, const char *s2 )
{
char *p = s1 + strlen( s1 );
while ( ( *p++ = *s2++ ) );
return s1;
}
int main(void)
{
enum { N = 14 };
char s1[N] = "first";
char *s2 = "second";
puts( strCat( s1, s2 ) );
return 0;
}
The program output is
firstsecond
On the other hand if you are already using the standard C function strlen then why not to use another standard C function strcpy?
With this function your function could be defined more simpler like
char * strCat( char *s1, const char *s2 )
{
strcpy( s1 + strlen( s1 ), s2 );
return s1;
}
If you want to build a new character array that contains two strings one appended to another then the function can look for example the following way.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * strCat( const char *s1, const char *s2 )
{
size_t n1 = strlen( s1 );
char *result = malloc( n1 + strlen( s2 ) + 1 );
if ( result != NULL )
{
strcpy( result, s1 );
strcpy( result + n1, s2 );
}
return result;
}
int main(void)
{
char *s1 = "first";
char *s2 = "second";
char *result = strCat( s1, s2 );
if ( result ) puts( result );
free( result );
return 0;
}
Again the program output is
firstsecond
Of course calls of the standard C function strcpy you can substitute for your own loops but this does not make great sense.
If you are not allowed to use standard C string functions then the function above can be implemented the following way.
#include <stdio.h>
#include <stdlib.h>
char * strCat( const char *s1, const char *s2 )
{
size_t n = 0;
while ( s1[n] != '\0' ) ++n;
for ( size_t i = 0; s2[i] != '\0'; )
{
n += ++i;
}
char *result = malloc( n + 1 );
if ( result != NULL )
{
char *p = result;
while ( ( *p = *s1++ ) != '\0' ) ++p;
while ( ( *p = *s2++ ) != '\0' ) ++p;
}
return result;
}
int main(void)
{
char *s1 = "first";
char *s2 = "second";
char *result = strCat( s1, s2 );
if ( result ) puts( result );
free( result );
return 0;
}
I have changed your program to look like below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char* strcaT();
char* a = "first";
char* b = "second";
printf("%s",strcaT(a,b));
return 0;
}
char* strcaT(char *t, char *s)
{
char* a = (char*)malloc(sizeof(char)*(strlen(t) + strlen(s) + 1));
for(int i=0; i<strlen(t); i++) {
a[i] = t[i];
}
for(int i=0; i<strlen(s); i++) {
a[strlen(t) + i] = s[i];
}
a[strlen(t)+strlen(s)] = '\0';
return a;
}
You are getting segfault because you are returning address of a local array which is on stack and will be inaccessible after you return. Second is that your logic is complicated to concatenate the strings.

returning string of chars in C

i compiled my piece of code and it worked fine using printf , but i want it to be returned without being printed ..
char *ft_strrev(char *str)
{
int i = 0;
while (str[i] != '\0')
{
i++;
}
while (i != 0)
{
i--;
}
return str;
}
int main ()
{
char *string;
string = "amrani";
ft_strrev(string);
}
The main thing here is to reverse the input entred ..
how can i exactly use return , to return the full char given to my var string , any tips ?
There are two approaches to doing this: make a new string and return it or mutate the parameter in place. Here's a new string version per your clarification comment. Note that memory is allocated for the new string and the caller is expected to free the result:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *ft_strrev(char *str)
{
int len = strlen(str);
char *reversed = malloc(sizeof(*reversed) * (len + 1));
reversed[len] = '\0';
while (*str != '\0')
{
reversed[--len] = *str++;
}
return reversed;
}
int main()
{
char *string = "amrani";
char *reversed = ft_strrev(string);
printf("%s\n", reversed);
free(reversed);
}
Note that many functions of this kind will include the length of the string as a second parameter so the function needn't call strlen.
This solution inverts the string in place.
#include <stdio.h>
#include <string.h>
char *ft_strrev(char *str)
{
int len = strlen(str);
for (int i = 0; i < len / 2; i++)
{
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
return str;
}
int main()
{
char string[] = "amrani";
ft_strrev(string); // return value of ft_strrev not used here
printf("%s", string);
}
Be aware of the difference betwween this:
char string[] = "amrani"; // string is an array of chars initialized with "amrani"
and this:
char *string = "amrani"; // string is a pointer to the string literal "amrani"
Modifying a string literal results in undefined behaviour, most likely some crash on modern platforms.

C - Delete spaces around specific char in string

I have a string of 80 chars (line from .txt file)
Somewhere at the end of the string I have numbers or strings or chars and "," (comma) between them. I need to delete these spaces around "," so I will be able to get them by strtok().
Any ideas ?
For example :
String : " name: today 12 ,r ,ab, 5 , seven"<br>
I need : " name: today 12,r,ab,5,seven"
You can apply this algorithm ::
Find the element , in this case a space.
Replace the element with an element of your choice, in this case an empty character.
This function might come handy for replacing any character to a string. You might add the char *replace function as a snippet and use it later for similar purposes.
char *replace(const char *the_input_string, char the_character,
const char *replacing_string)
{
int count = 0;
const char *t;
for(t=the_input_string; *t; t++)
count += (*t == the_character);
size_t rlen = strlen(replacing_string);
char *res = (char*)malloc(strlen(the_input_string) + (rlen-1)*count + 1);
char *ptr = res;
for(t=the_input_string; *t; t++)
{
if(*t == the_character)
{
memcpy(ptr, replacing_string, rlen);
ptr += rlen;
}
else
*ptr++ = *t;
}
*ptr = 0;
return res;
}
Driver Program ::
int main(int argc, char const *argv[])
{
const char *s = replace("name: today 12 ,r ,ab, 5 , seven", ' ', "");
printf("%s\n", s);
return 0;
}
Please refer to this link and the code might be verisimilar but use the above code as the solution mentioned there might throw some errors or warnings.
Because the resulting string will be shorter then the original string, you can do the replacement in place: When you find a comma, copy it and skip the following space. To treat the space before the comma, keep track of the first space after the last non-space character and skip that, too if necessary:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
void remspc(char *str)
{
char *first = str; // dest. char after last non-space
char *q = str; // destination pointer
// skip leading white space
while (isspace((unsigned char) *str)) str++;
while (*str) {
if (*str == ',') {
q = first; // skip space before comma
*q++ = *str++;
first = q;
// skip space after comma
while (isspace((unsigned char) *str)) str++;
} else {
// remember last non-space
if (!isspace((unsigned char) *str)) first = q + 1;
*q++ = *str++;
}
}
*first = '\0';
}
int main(void)
{
char str[] = " name: today 12, r ,ab, , 5 , seven";
remspc(str);
puts(str);
return 0;
}
This solution will run commas that are separated by white space together, which may lead to problems with strtok, because it will consider stretches of commas as a single delimiter.
You may give this a try!
Replace with your code where necessary.
#include <stdio.h>
#include <stdlib.h>
int main()
{ int i;
char line[] = "name: today 12 ,r ,ab, 5 , seven";
int length = strlen(line);
char line2[length];
for(i = 0; i<length; i++) {
if(!isspace(line[i])) {
line2[i] = line[i];
}
}
for(i = 0; i<length; i++){
printf("%c", line2[i]);
}
return 0;
}

Resources