Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:30:24+00:00 2026-06-02T06:30:24+00:00

char punct(char a[], int len) { for (int i = 0; i < len;

  • 0
char punct(char a[], int len) {
    for (int i = 0; i < len; i++) {
        if (ispunct(a[i]))
            { return i; }
        return -1;
    }
}

Could anyone tell me why this function is only returning -1 for every string that I hand to it? I’m writing a program to take words from a .txt file and outputting them (with a little bit of alteration) into a different .txt file, and I need to remove punctuation from the strings. However, I cannot seem to detect where the punctuation is using the ispunct() function. I even wrote my own function using all of the cases of punctuation I could imagine and it still only returns -1. Is it a problem with the function here or the strings that I am giving to it? If showing more of my code is necessary, please let me know. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:30:27+00:00Added an answer on June 2, 2026 at 6:30 am

    That’s because if ispunct(a[0]) returns false, return -1 is called.

    You should move the default return outside of the loop:

    char punct(char a[], int len) {
        for (int i = 0; i < len; i++) {
            if (ispunct(a[i]))
                { return i; }
        }
        return -1;
    }
    

    Note that this function only returns the first punctuation index, not all indexes. I guess you’re treating this case outside the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

char ParseCmd(char *buf,int len) { char *p; p = strtok(buf, ); return *p; }
char *strtok(char *s1, const char *s2) repeated calls to this function break string s1
char bytes[0x7FFFFFFF]; /* this? */ int main() { return 0; } Over 2Gb? (if
char c='c'; int i=10; double d =50; long l=30; String s=Goodbye; Are these statement
char character = 'c'; string str = null; str = character.ToString();//this is ok char[]
char a[133]; char x[4]=ccc; int line=3,count=1; //string aa; //cin>>aa; ifstream out(text); while(out.getline(a,4,':')&&(strncmp(a,x,4))) {cout<<hi; cout<<a<<endl;
char *funcNames[]= {VString,VChar,VArray,VData}; for(int i=0;i<4;i++) { char* temp = funcNames[i]; int len = strlen(funcNames[i]);
char **Data[70]={NULL}; What is the correct terminology for this? How else could it be
char *str = NULL; str = Operation(argv[1]); cout << Cal(str); Operation function return a
char *getInput2(char *param) { int data_len; data_len=atoi(getenv(CONTENT_LENGTH)); char input[9]; fgets(input, 9, stdin); return strdup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.