C/String/String Length

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

Add null terminator to string end

#include <stdio.h>
int main(void)
{
  char s[256], *p;
  p = s;
  while((*p++ = getchar())!= "\n") ;
  *p = "\0"; /* add null terminator */
  printf(s);
  return 0;
}


Check string length and set string end

#include <stdio.h>
#include <string.h>
int main(void)
{
  char str[10];
  int i;
  printf("Enter a string: ");
  fgets(str, 10, stdin);
  /* remove newline, if present */
  i = strlen(str) - 1;
  
  if(str[i] =="\n") 
      str[i] = "\0";
  printf("This is your string: %s", str);
  return 0;
}


Computes the length of a line

#include <string.h>
#include <stdio.h>
int main()
{
    char line[100];
    printf("Enter a line: ");
    fgets(line, sizeof(line), stdin);
    printf("The length of the line is: %d\n", strlen(line));
    return (0);
}


Get length of substring composed of given characters

#include <string.h>
#include <stdio.h>
int main(void)
{
  int len;
  len = strspn("this is a test", "siht ");
  printf("%d", len);
  return 0;
}


Get length of substring composed of given characters: how to use strspn

#include <stdio.h>
#include <string.h>
int main ()
{
  int i;
  char key[] = "890th";
  char str[] = "1234567890";
  i = strspn (key, str);
  
  printf ("Length of initial number is %d\n", i);
  return 0;
}


Get the string length

#include <stdio.h>
#include <string.h>
int main(void)
{
  char str[80];
  gets(str);
  printf("Length is %d", strlen(str));
  return 0;
}


Lengths of strings

#include <stdio.h>
void main() {
  char str1[40] = "To be or not to be";
  char str2[40] = ",that is the question. ";
  int count = 0;                
  while (str1[count] != "\0")   
    count++; 
  printf("\nThe length of the string \"%s\" is %d characters.", str1, count);
  count = 0; 
  while (str2[count] != "\0")
    count++;
  printf("\nThe length of the string \"%s\" is %d characters.\n", str2, count);
}


print the string forward and backwards

#include <stdio.h>
#include <string.h>
char *p = "hello world";
int main(void)
{
  register int t;
  printf(p);
  
  for( t = strlen( p ) - 1; t > -1; t--) 
      printf("%c", p[ t ]);
  return 0;
}


Return string length: how to use strlen

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[256];
  printf ("Enter a sentence: ");
  gets (str);
  printf ( "%s is %u characters long\n", str, strlen( str ) );
  return 0;
}


String length

#include <stdio.h>
int length(char string[]) {
    int index;
    for (index = 0; string[index] != "\0"; ++index)
        continue;
    return (index);
}
int main()
{
     char line[100];
     while (1) {
        printf("Enter line:");
        fgets(line, sizeof(line), stdin);
      printf("Length (including newline) is: %d\n", length(line));
    }
}


String operator: get length, compare and find a char

#include <stdio.h>
#include <string.h>
int main(void)
{
  char s1[80], s2[80];
  gets(s1);
  gets(s2);
  printf("lengths: %d %d\n", strlen(s1), strlen(s2));
  if(!strcmp(s1, s2)) 
      printf("The strings are equal\n");
  strcat(s1, s2);
  printf("%s\n", s1);
  strcpy(s1, "This is a test.\n");
  printf(s1);
  
  if(strchr("hello", "e")) 
       printf("e is in hello\n");
 
  if(strstr("hi there", "hi")) 
      printf("found hi");
  return 0;
}