简体   繁体   中英

How to define "strchr(s,oldch)" in function definition in c program?

I am a beginner to C language. I dont understand the function defintion part. What does "strchr(s,oldch)" do? I am trying to convert it into ctypes program.

#include <stdio.h>
#include <string.h>
#include <math.h>

/* Replace och with nch in s and return the number of replacements */
extern int replace(char *s, char och, char nch);

/* Replace a character in a string */
int replace(char *s, char oldch, char newch) {
int nrep = 0;
while (s = strchr(s,oldch)) {
*(s++) = newch;
nrep++;
}
return nrep;
}

/* Test the replace() function */
{
char s[] = "Skipping along unaware of the unspeakable peril.";
int nrep;
nrep = replace(s,' ','-');
printf("%d\n", nrep);
printf("%s\n",s);
}

what does while (s = strchr(s,oldch)) mean? what work it does? How to write it in other ways? Can anyone explain it?

The C library function char strchr(const char *str, int c) searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str .

strchr() function checks whether the original string contains defined characters. If the character is found inside the string, it returns a pointer value; otherwise, it returns a null pointer.

Syntax:

char *strchr(const char *str, int c)

Parameters

str − This is the C string to be scanned.
c − This is the character to be searched in str.

Example. The following example shows the usage of strchr() function.

#include <stdio.h>
#include <string.h>

int main () {
   const char str[] = "www.ted.com";
   const char ch = '.';
   char *ret;

   ret = strchr(str, ch);

   printf("String after |%c| is - |%s|\n", ch, ret);
   
   return(0);
}

compile and run the above program that will produce the following result:

String after |.| is - |.ted.com|

The C library function strchr loops through an array of characters and returns the first occurrence of a certain character. In your case, you're looping through an array of characters(string) and replacing oldch with newch , then you're returning the total number of characters replaced in your string:-

/*  
    Declaring a function called replace that takes as input 3 
    arguments:- 
    > a string 's',
    > character to be replaced in the string 'och'
    > what to replace with 'nch'
*/
extern int replace(char *s, char och, char nch);

int replace(char *s, char oldch, char newch) {
    //initialize our counter to keep track of characters replaced
    int nrep = 0;
    /*
      call the function strchr and try to find if the next position of 
      the character we'd like to replace can be located, i.e. there's 
      still more old characters left in the string. If this is the 
      case, replace this character with 'newch' and continue doing this 
      until no more old characters can be found in the string, at which 
      point you return total number of old characters replaced (nrep).
    */
    while (s = strchr(s,oldch)) {
        //replace current oldch with newch and find next oldch
        *(s++) = newch;
        //increment number of characters replaced
        nrep++;
    }
    return nrep;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM