C Tutorial/String/String Terminator

Материал из C\C++ эксперт
Перейти к: навигация, поиск

By setting first[4] to NULL ("\0"), we can shorten the string

<source lang="cpp">#include <string.h>

   #include <stdio.h>
   int main()
   {
       char name[30];
       strcpy(name, "Saaaaaaaaaaaaaaam");
       printf("The name is %s\n", name);
       name[4] = "\0";
       printf("The name is %s\n", name);
       return (0);
   }</source>
The name is Saaaaaaaaaaaaaaam
The name is Saaa

Displaying a string with a string terminator in the middle

<source lang="cpp">#include <stdio.h> int main(void) {

 printf("The character \0 is used to terminate a string.");
 return 0;

}</source>

The character

Overwrite the newline character in string

<source lang="cpp">#include <stdio.h>

  1. include <string.h>
  2. include <ctype.h>

int main(void) {

 char text[100];                
 printf("\nEnter the string to be searched(less than 100 characters):\n");
 fgets(text, sizeof(text), stdin);
 text[strlen(text)-1] = "\0";
 printf("%s",text);

}</source>

Enter the string to be searched(less than 100 characters):
     string
     string