Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,024 questions

51,976 answers

573 users

How to remove punctuation from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void remove_punctuation_from_string(char* p) {
    char* src = p, * dest = p;

    while (*src) {
        if (ispunct((unsigned char)*src)) {
            src++; // Skip punctuation character
        }
        else if (src == dest) {
            src++;
            dest++;
        }
        else {
            *dest++ = *src++; // copy not punctuation character
        }
    }

    *dest = 0;
}

int main()
{
    char str[] = "C@ C++, Java: (Python) C#$ Go! <Rust>";

    printf("len = %zu\n", strlen(str));

    remove_punctuation_from_string(str);

    printf("str = %s\n", str);
    printf("len = %zu\n", strlen(str));

    return 0;
}



/*
run:

len = 37
str = C C Java Python C Go Rust
len = 25

*/

 



answered Jun 21, 2024 by avibootz
edited Jun 21, 2024 by avibootz

Related questions

1 answer 86 views
2 answers 102 views
2 answers 179 views
1 answer 87 views
1 answer 81 views
1 answer 78 views
1 answer 123 views
...