I am trying to write an function, which takes an string as input and do something and return the strings.
Here Is a small try from my side.

#include<stdio.h>

char algo(char n)
 {
  char result[15];
  if(n=='1' || n=='2' || n=='3' || n=='4' || n=='5' || n=='6')
        {
          result = n;
        }
  else
        {
          result = '10';
        }

        return result;

 }

I am new to C.

Not really sure what the question is...but you functions takes a character not a string.

Not really sure what the question is...but you functions takes a character not a string.

I am new to C so my code can be wrong. But what I want is to write a function which takes a string as input, do some inside work and return the string.

Strings don't need to be returned from a function.

When you send a string to a function (which is what you want to do), you are sending a constant pointer (which is the name of the char array, itself).

And since the function has the address, rather than just the variable itself( like a number would be), then it can make changes into the char array, (the string).

#include <stdio.h>

void changeIt(char *mystring) {
   mystring[2] = 't';  
}


int main() {
  char mystring[10] = { "Ack!" };
  
  changeIt(mystring);
  printf("\n%s", mystring);

  return 0;
}

So normally, you want to bring the array name into the function that will be working on it. Local arrays from the function itself, will terminate after the function executes - so trying to use it afterward is an error - sometimes you get away with it, but static local arrays, can't be trusted to work right, after their function is done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.