Showing posts with label string function in c. Show all posts
Showing posts with label string function in c. Show all posts

Sunday, May 17, 2020

,

miscellaneous function in c programming (scanf(),printf(),sprintf() and more)

miscellaneous function in c programming (scanf(),printf(),sprintf() and more)
miscellaneous function in c programming (scanf(),printf(),sprintf() and more)

printf() Function in c programming

printf is a predefined function in "stdio.h" header file with the help of this function we can print data or user-defined messages on a computer screen. printf() function can take a number of argument and every argument should separate with a comma (, ) Within the double cotes but the first argument must be within the double cotes (" ").

Syntax : - 

 printf("user defined message"); 
 or 
 prinf("Format specifers",value1,value2,..); 

examples for printf() function in c programming  : -  

int a=20;
double d=12.4;
printf("%f%d",d,a);

scanf() Function in c programming:-

 scanf() is a predefined function in "stdio.h" header file. It can be used to read the input value from the keyword.

Syntax: - 

scanf("format specifiers",&value1,&value2,.....);

examples for scanf() function in c programming  : - 

int a;
float b;
scanf("%d%f",&a,&b);

sprintf() function in c programming

sprintf() function is as printf() but insted of sending output to console it returns the formatted string.

Syntax: -
 int sprintf(char *str, const char *control_string, [ arg_1, arg_2, ... ]); 

The first argument in sprintf() function is a pointer which is pointed out towards the target string. All other arguments are the same as for printf() function in c.

sprintf() function write the data in string pointer str and return the number of characters written to str excluding the null character. if an error occurs in operation it returns -1.

  • Example to learn about how to use sprintf() function:-


#include <stdio.h>
#include <string.h>
int factorial(int );
 
int main()
{
 
    int sal;
    char name[30], designation[30], info[60];
 
    printf("your name: ");
    gets(name);
 
    printf("your designation: ");
    gets(designation);
 
    printf("your salary: ");
    scanf("%d", &sal);
 
    sprintf(info, "Welcome %s !\nName: %s \nDesignation: %s\nSalary: %d",
        name, name, designation, sal);
 
    printf("\n%s", info);
 
    // signal to operating system program ran fine
    return 0;
}

memcpy() function in c programming :-

memcpy() function in c programming using to copy n characters from the object pointed to s2 in the object pointed by s1. It returns a pointer to the destination. 

if objects overlap then memcpy() function may not work. 

  The syntax for c language: - 

void *memcpy(void *s1, const void *s2, size_t n);

s1:- an array where s2 will be copied.

s2:- a string that is going to copy in s1.

n:- number of characters to copy.

NOTE : - <string.h> header file should be included 

  • Example for memcpy() function

/* Example using memcpy by TechOnTheNet.com */

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

int main(int argc, const char * argv[])
{

    return 0;
}

memmove() function in c programming :-

memmove() function in c programming use to copy n characters from the object pointed to s2 in the object pointed by s1. its returns a pointer to the destination.

memmove() function in c programming will work for objects overlap. 

Syntax: -

void *memmove(void *s1, const void *s2, size_t n);

memchr() function in c programming :-

memchr() function in c programming using to searches a character in an object and return a pointer for that character.

 Syntax :- 

void *memchr(const void *s, int c, size_t n);
  •  Example for memchr() function in c : - 
/* Example using memchr by functioninprogramming.com */

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

int main(int argc, const char * argv[])
{
    char search[] = "functioninprogramming";
    char *ptr;

    /* Return a pointer to the first 'm' within the search string */
    ptr = (char*)memchr(search, 'm', strlen(search));

    /* If 'm' was found, print its location (This should produce "17") */
    if (ptr != NULL) printf("Found 'm' at position %ld.\n", 1+(ptr-search));
    else printf("'m' was not found.\n");

    return 0;
}

memset() function in c programming:-

memset() function in c programming use to stores a character into the first n character of object.

 syntax :- 

void *memset(void *s, int c, size_t n);

 strpbrk() function in c programming : - 

strbrk() function in c used to search a string for one of the set of characters. 

Syntax: - 

char *strpbrk(const char *s1, const char *s2);

 strbrk() function return a pointer of leftmost matching characterin the string. If due to any reason match not found  then it return NULL pointer.

 strspn() function in c  :- 

strspn() function in c programming is used to Search String for Initial Span of Characters in Set and return the index of the first character that is not in the set.

 strtok() function in c  : -

strtok() function in c programming splits the function in the number of tokens and returns a pointer to the first character of the token.

strtok() function stores a null character as the end of tokens as a termination.

syntax: - 

char *strtok(char *s1, const char *s2);

we hope these all function will help you to learn about c programming and its functions.

Saturday, May 16, 2020

,

string funcation like strcmp, strcpy, strlen and more in c



Learn about string function in c programming and how to perform various operation on string. We will use pre-defined function to perform those operation on the string in c programming


string funcation like strcmp, strcpy, strlen and more in c
string function like strcmp, strcpy, strlen and more in c


You will learn about how to compare two stings (strcmp function), concatenate strings (strcat function), copy (strcpy function) and many more operations using a pre-define function in c programming lets start with function.


First Declare String in c programming : -  

Method 1 - 

char address[]={'T', 'E', 'X', 'A', 'S', '\0'};

Method 2 - 


char address[]="Function In Programming";

NULL character (\0)  automatically at the end of any string.


  • strlen String Function in c - 


size_t strlen(const char *str)

size_t represents unsigned short
strlen function uses to count the length of any string without end character in c programming.

Example:-

#include 
#include 
int main()
{
     char str1[20] = "Function in programming";
     printf("Length of string str1: %d", strlen(str1));
     return 0;
}

sizeof function in c programming returns the value of total allocated size which is assigned in the array.


  • strnlen String Function in c - 

Syntax for strnlen -

size_t strnlen(const char *str, size_t maxlen)

size_t represents unsigned short
This function returns the length of a string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.

Example - 


#include 
#include 
int main()
{
     char str1[20] = "functioninc";
     printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));
     printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));
     return 0;
}

Output - 
String length is 13 when maxlen is 30
String length is 10 when maxlen is 10

In the second string maxlen is 10 so its gives 10.
  • strcmp String Function in c - 

strcmp function compare two string and give integer value. If both string are same then it gives 0 otherwise it may return negative and positive values depends on the comparison.

Example - 


#include 
#include 
int main()
{
     char s1[20] = "functioninprogramming";
     char s2[20] = "functioninc";
     if (strcmp(s1, s2) ==0)
     {
        printf("string 1 and string 2 are equal");
     }else
      {
         printf("string 1 and 2 are different");
      }
     return 0;
}

  • strncmp String Function in c - 

int strncmp(const char *str1, const char *str2, size_t n)

size_t represents unsigned short
strncmp function compare two string for first n character for both string.

Example - 


#include 
#include 
int main()
{
     char s1[20] = "program";
     char s2[20] = "program";
     /* below it is comparing first 8 characters of s1 and s2*/
     if (strncmp(s1, s2, 7) ==0)
     {
         printf("string 1 and string 2 are equal");
     }else
     {
         printf("string 1 and 2 are different");
     }
     return 0;
}

Output
Both strings are same


  • strcat String Function in c - 

Syntax 

char *strcat(char *str1, char *str2)

This function merge two string and return concatenated string.

Example - 


#include 
#include 
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strcat(s1,s2);
     printf("Output string after concatenation: %s", s1);
     return 0;
}

Output

HelloWorld

  • strncat String Function in c -

This function merges n character of two string and return concatenated string.

 Example -

#include 
#include 
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strncat(s1,s2, 3);
     printf("Concatenation using strncat: %s", s1);
     return 0;
}

OUTPUT - 

Hellowor ( Where n is 3)

  •  strcpy String Function in c -


char *strcpy( char *str1, char *str2)

This function use to copy string str2 in str1 with (terminator char /0)

 Example - 

#include 
#include 
int main()
{
     char s1[30] = "string 1";
     char s2[30] = "string 2 : I’m gonna copied into s1";
     /* this function has copied s2 into s1*/
     strcpy(s1,s2);
     printf("String s1 is: %s", s1);
     return 0;
}

OUTPUT : -

 String s1 is
string 2 : I’m gonna copied into s1

  •  strncpy String Function in c -

char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned n number.


This function has two cases: - 
Case 1 : -  str2 > n then it just copies n number of character in str1 from str2.
Case 2 : - str2<n then it copies all the character in str1 from str2 and appends several terminator chars(‘\0’) in empty space because n is greater the str2.

Example of strncpy: 

#include 
#include 
int main()
{
     char first[30] = "string 1";
     char second[30] = "string 2: I’m using strncpy now";
     /* this function has copied first 10 chars of s2 into s1*/
     strncpy(s1,s2, 12);
     printf("String s1 is: %s", s1);
     return 0;
}

OUTPUT : - 
 str1 is
I’m using st

  •  strchr String Function in c - 

This function used to search string str for character ch

 Example of strchr : -


#include 
#include 
int main()
{
     char mystr[30] = "this is it";
     printf ("%s", strchr(mystr, 'i'));
     return 0;
}

Output : -

strchr gives it from mystr for i.

  •  strrchr String Function in c -

This function is  similar to the function strchr, the only difference is that function strrchr will search a string in reverse order let see the example : -

#include 
#include 
int main()
{
     char mystr[30] = "this is it";
     printf ("%s", strrchr(mystr, 'f'));
     return 0;
}

Output : -

strchr gives it from mystr for i.

  •  strstr String Function in c -


char *strstr(char *str, char *srch_term)

strstr function is similar to the strchr, the only difference is that it searches for a string instead of single char like strchr.

Example for strstr : -  
 

#include 
#include 
int main()
{
     char inputstr[70] = "String in C at funcationinprogramming.com";
     printf ("Output string is: %s", strstr(inputstr, 'func'));
     return 0;
}

Output : - 

String is funcationinprogramming

These all are the string functions in c programming you can learn about them and make a use of them to save time in programming.