How to Clear a Cstring to Accept Input Again

String is an array of characters. In this guide, we acquire how to declare strings, how to work with strings in C programming and how to use the pre-divers string handling functions.

Nosotros will run across how to compare ii strings, concatenate strings, copy one string to another & perform various string manipulation operations. We tin can perform such operations using the pre-divers functions of "string.h" header file. In order to use these cord functions you must include string.h file in your C plan.

String Declaration

string declaration in C
Method 1:

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

Method 2: The above cord can also be defined as

char address[]="TEXAS";

In the higher up declaration Zip character (\0) will automatically be inserted at the stop of the cord.

What is Nothing Char "\0"?
'\0' represents the end of the string. Information technology is likewise referred every bit String terminator & Cipher Graphic symbol.

String I/O in C programming

C String-IO

Read & write Strings in C using Printf() and Scanf() functions

#include <stdio.h> #include <cord.h> int main() {     /* String Annunciation*/     char nickname[20];      printf("Enter your Nick name:");      /* I am reading the input string and storing information technology in nickname      * Array proper name alone works as a base of operations address of array so      * we tin can use nickname instead of &nickname here      */     scanf("%southward", nickname);      /*Displaying String*/     printf("%s",nickname);      render 0; }          

Output:

Enter your Nick name:Negan Negan

Annotation: %s format specifier is used for strings input/output

Read & Write Strings in C using gets() and puts() functions

#include <stdio.h> #include <string.h> int main() {     /* String Declaration*/     char nickname[20];      /* Panel brandish using puts */     puts("Enter your Nick name:");      /*Input using gets*/     gets(nickname);      puts(nickname);      render 0; }

C – Cord functions

C string-functions

C String office – strlen

Syntax:

size_t strlen(const char *str)

size_t represents unsigned short
It returns the length of the cord without including end grapheme (terminating char '\0').

Instance of strlen:

#include <stdio.h> #include <string.h> int master() {      char str1[xx] = "BeginnersBook";      printf("Length of string str1: %d", strlen(str1));      return 0; }

Output:

Length of string str1: 13

strlen vs sizeof
strlen returns you the length of the cord stored in array, even so sizeof returns the total allocated size assigned to the array. So if I consider the above example again so the following statements would return the below values.

strlen(str1) returned value thirteen.
sizeof(str1) would return value xx as the array size is 20 (see the first argument in main function).

C Cord office – strnlen

Syntax:

size_t strnlen(const char *str, size_t maxlen)

size_t represents unsigned short
It returns length of the cord if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.

Case of strnlen:

#include <stdio.h> #include <cord.h> int principal() {      char str1[twenty] = "BeginnersBook";      printf("Length of string str1 when maxlen is thirty: %d", strnlen(str1, 30));      printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));      return 0; }

Output:
Length of string str1 when maxlen is 30: 13
Length of string str1 when maxlen is x: 10

Have you noticed the output of 2nd printf argument, even though the string length was thirteen it returned but 10 because the maxlen was x.

C Cord function – strcmp

int strcmp(const char *str1, const char *str2)

Information technology compares the 2 strings and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise information technology may return a negative or positive value based on the comparing.

If string1 < string2 OR string1 is a substring of string2 then it would result in a negative value. If string1 > string2 then it would return positive value.
If string1 == string2 then yous would get 0(zip) when you utilize this function for compare strings.

Example of strcmp:

#include <stdio.h> #include <string.h> int main() {      char s1[20] = "BeginnersBook";      char s2[20] = "BeginnersBook.COM";      if (strcmp(s1, s2) ==0)      {         printf("cord ane and string 2 are equal");      }else       {          printf("string ane and 2 are different");       }      return 0; }

Output:

string one and two are different

C String function – strncmp

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

size_t is for unassigned short
It compares both the string till n characters or in other words it compares first northward characters of both the strings.

Example of strncmp:

#include <stdio.h> #include <string.h> int main() {      char s1[20] = "BeginnersBook";      char s2[twenty] = "BeginnersBook.COM";      /* below it is comparison offset viii characters of s1 and s2*/      if (strncmp(s1, s2, 8) ==0)      {          printf("string 1 and string 2 are equal");      }else      {          printf("cord 1 and 2 are different");      }      render 0; }

Output:

string1 and string 2 are equal

C String function – strcat

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

It concatenates two strings and returns the concatenated string.

Example of strcat:

#include <stdio.h> #include <string.h> int principal() {      char s1[10] = "Hello";      char s2[10] = "Earth";      strcat(s1,s2);      printf("Output string after concatenation: %s", s1);      return 0; }

Output:

Output cord afterwards concatenation: HelloWorld

C String office – strncat

char *strncat(char *str1, char *str2, int north)

Information technology concatenates northward characters of str2 to string str1. A terminator char ('\0') will always be appended at the terminate of the concatenated cord.

Case of strncat:

#include <stdio.h> #include <string.h> int main() {      char s1[10] = "Hi";      char s2[10] = "World";      strncat(s1,s2, 3);      printf("Concatenation using strncat: %s", s1);      render 0; }

Output:

Concatenation using strncat: HelloWor

C Cord function – strcpy

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

It copies the string str2 into string str1, including the end character (terminator char '\0').

Example of strcpy:

#include <stdio.h> #include <string.h> int principal() {      char s1[30] = "string 1";      char s2[30] = "string two : I'm gonna copied into s1";      /* this function has copied s2 into s1*/      strcpy(s1,s2);      printf("Cord s1 is: %due south", s1);      render 0; }

Output:

String s1 is: string ii: I'one thousand gonna copied into s1

C String function – strncpy

char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned short and n is a number.
Case1: If length of str2 > n and so it but copies first n characters of str2 into str1.
Case2: If length of str2 < north then information technology copies all the characters of str2 into str1 and appends several terminator chars('\0') to accumulate the length of str1 to make it north.

Example of strncpy:

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

Output:

String s1 is: string 2: I'm

C String function – strchr

char *strchr(char *str, int ch)

Information technology searches string str for grapheme ch (you may exist wondering that in in a higher place definition I have given information type of ch as int, don't worry I didn't make any mistake it should be int but. The affair is when nosotros give any grapheme while using strchr and so it internally gets converted into integer for meliorate searching.

Case of strchr:

#include <stdio.h> #include <string.h> int main() {      char mystr[30] = "I'm an example of office strchr";      printf ("%s", strchr(mystr, 'f'));      return 0; }

Output:

f office strchr

C String function – Strrchr

char *strrchr(char *str, int ch)

It is similar to the function strchr, the only difference is that it searches the string in opposite order, now you lot would have understood why we have actress r in strrchr, aye you lot guessed it correct, it is for reverse only.

Now permit's have the same higher up case:

#include <stdio.h> #include <string.h> int main() {      char mystr[thirty] = "I'm an example of part strchr";      printf ("%s", strrchr(mystr, 'f'));      render 0; }

Output:

role strchr

Why output is dissimilar than strchr? Information technology is because information technology started searching from the end of the string and found the kickoff 'f' in role instead of 'of'.

C Cord office – strstr

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

It is similar to strchr, except that it searches for cord srch_term instead of a unmarried char.

Example of strstr:

#include <stdio.h> #include <string.h> int main() {      char inputstr[70] = "String Function in C at BeginnersBook.COM";      printf ("Output string is: %s", strstr(inputstr, 'Begi'));      return 0; }

Output:

Output string is: BeginnersBook.COM

You lot can also utilise this function in identify of strchr as you lot are allowed to give single char besides in place of search_term cord.

spencerelithe.blogspot.com

Source: https://beginnersbook.com/2014/01/c-strings-string-functions/

0 Response to "How to Clear a Cstring to Accept Input Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel