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 7022563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:35:55+00:00 2026-05-27T23:35:55+00:00

I am searching a string and the string could contain a newline or a

  • 0

I am searching a string and the string could contain a newline or a CRLF. I increment the pointer to search through each character. I have converted the hex to decimal. However, as I am looking for a single char, I am not sure this would be the best way to do this.

Many thanks for any suggestions,

   /*
      0x0a (ASCII newline)
      0x0d (ASCII carriage return)
      CRLF (0x0d0a)
    */

    while(*search != '\0') {
        /* Seach for a newline */
        if(*search == 10) {
            printf("\nnewline Found\n");
        }

        /* Search for a CRLF */
        if(*search == 3338) {
            printf("\nCRLF Found\n");
        }

        search++;
    }

I’m compiling with gcc 4.6.2 in C89-mode.

  • 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-05-27T23:35:55+00:00Added an answer on May 27, 2026 at 11:35 pm

    CRLF is two characters: CR, and LF. You need to match them both individually, not together as you’ve done with 3338 (I think: where did you get that number?) Also, use the character constants: '\0', '\n', '\r', it will make your code clearer.

    /*
      0x0a (ASCII newline)
      0x0d (ASCII carriage return)
      CRLF (0x0d0a)
    */
    
    while(*search != '\0') {
        /* Seach for a newline */
        if(*search == '\n') {
            printf("\nnewline Found\n");
            search++;
        }
    
        /* Search for a CR or a CRLF */
        if(*search == '\r') {
            // OK, we found a CR, is it followed by a LF?
            if(*(search + 1) == '\n') {
                // Yes, it is, thus, it is a CRLF
                printf("\nCRLF Found\n");
                search += 2; // Note the 2! CRLF is 2 characters!
            }
            else {
                // No, just a lonely CR, forever alone.
                printf("\nCarriage return found\n");
                search++;
            }
        }
    }
    

    As another poster suggests, you can also use the strchr and strstr functions, and take the minimum valid result from them. (It’ll be like 3 lines of code.)

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

Sidebar

Related Questions

How could I search for string in text file in java? Would it have
I am searching a character at first occurence in string using the following code.
I'm studying string searching algorithms now and wondering what algorithm is used for .NET
For the two string searching algorithms: KMP and suffix tree, which is preferred in
I need to create an array by searching a string for occurrences of '['
Searching for a string within a string is extremely well supported in .NET but
I am searching for a string in Perl and storing it in another scalar
I am searching for the string <!--m--><li class=g w0><h3 class=r><a href= within the HTML
I was searching here about converting a string like 16:20 to a DateTime type
Been searching the net for an example of how to convert HTML string markup

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.