Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Program to count punctuation marks in the string using methods

+5 votes
asked Apr 7, 2019 by anonymous
We have to use only methods and string

1 Answer

0 votes
answered Feb 5 by Vipul Kumar (280 points)

FOR 'C'

 

#include <stdio.h>
#include <stdlib.h>
void main(){
    int n,i=0,j,p=0;
    char punctuation[] = {'!', '"', '#', '$', '%', '&','(', ')', '*', '+', ',', '-','.', '/', ':', ';', '<', '=','>', '?', '@', '[', '\\', ']','^', '_', '`', '{', '|', '}', '~'};
    printf("Enter the lenght of string you want to enter : ");
    scanf("%d",&n);
    getchar();
    n++;
    char *s=(char*)malloc(n*sizeof(char));
    printf("Enter the string : ");
    fgets(s,n,stdin);
    while (s[i]!='\0'){
        for (j=0;j<31;j++){
            if (s[i]==punctuation[j]){
                p++;
            }
        }
        i++;
    }
    printf("-------------------------------------------------------------------------------------------------------------------------------------");
    printf("\nThere are total %d punctuation marks in the string you entered",p);
    printf("\n-------------------------------------------------------------------------------------------------------------------------------------");
}

FOR 'PYTHON'

punctuation = ['!', '"', '#', '$', '%', '&', "'",'(', ')', '*', '+', ',', '-','.', '/', ':', ';', '<', '=','>', '?', '@', '[', '\\', ']','^', '_', '`', '{', '|', '}', '~']
a=input("Enter the string : ")
p=0
for i in range(len(a)):
    for j in range(len(punctuation)):
        if a[i]==punctuation[j]:
            p+=1
print("-------------------------------------------------------------------------------------------------------------------------------------")
print("There are",p,"punctuation marks in the string")
print("-------------------------------------------------------------------------------------------------------------------------------------")

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...