Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Sajib Datta Feb 21, 2014.  In the last class we discussed: ◦ Bubble sort  How it works  performance.

Similar presentations


Presentation on theme: "Dr. Sajib Datta Feb 21, 2014.  In the last class we discussed: ◦ Bubble sort  How it works  performance."— Presentation transcript:

1 Dr. Sajib Datta CSE@UTA Feb 21, 2014

2  In the last class we discussed: ◦ Bubble sort  How it works  performance

3  A string is a series of one or more characters, terminated by a null character.  ◦Example  ◦“I am at UTA”  The double quotation marks are not part of the string.  They just inform the compiler they enclose a string,  just as single quotation marks identify a character.

4  C does not have a string type.  Characters in a string are stored in adjacent memory cells, one character per cell, and an array consists of adjacent memory locations, so placing a string in an array is quite natural.  We use an array of chars for storing a string.  We can declare this just like we did with other types.  Example  char letters[12] = “I am at UTA”; IamatUTA\0

5  \0 is the null character which is used by C for marking the end of a string  The null character is not the digit zero; it is the nonprinting character.  Its ASCII code value is 0.  The presence of the null character means the array must have at least one more cell than the number of the characters to be stored

6  Even though we define a char array much larger than a string, the null character will tell the compiler where is the end of the string. This is useful when we call printf( ).  char letters[15] = “I am at UTA”; IamatUTA\0

7  gets() – read a line from input  Reads characters from stdin and stores them as a string into str until a newline character ('\n').  The ending newline character ('\n') is not included in the string.  A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.  Notice that gets() does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid overflows.

8  puts(the name of your char array) print out a string.  #include  int main(void)  {  char myString[41];  //printf("Please intput a string with characters <= 40:\n");  puts("Please intput a string with characters <= 40:\n");  gets(myString);  puts(myString);  return 0;  }  Note that all input will be regarded as one string.  However, scanf(“%s”, myString) will only read the characters until the first space as one string.

9  Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  scanf("%s", name);  printf("Hello, %s.\n", name);  return 0;  }   %s is the format specifier for a string.  The name of a char array represents an address in computer memory, therefore, we don‟t need “&” when we call scanf function.

10  If your input is ◦ Sajib Datta  Just calling scanf() once will only store Sajib in the char array as a string.  The remaining of the input will be ignored.  In this case, the space is used to separate strings in a sentence.

11  Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  gets(name);  printf("Hello, %s.\n", name);  return 0;  }   If you input “Sajib Datta”, it will print “Hello Sajib Datta.”

12 1. char a[2] = {‘x’,’y’}; printf(“%s”, a); // puts(a); 2. char a[3] = {‘x’,’y’}; printf(“%s”,a); //puts(a); 3. char a[2] = “xy”; printf(“%s”,a); //puts(a); 4. char a[3] = “xy”; printf(“%s”,a); //puts(a); 5. char a[5] = {‘’x’,’y’,’z’,’\0’,’d’}; printf(“%s”,a); //puts(a);

13 6. char a[5] = “xyz\0d”; printf(“%s”,a); //puts(a); 7. char a[5] = “xy\\0”; printf(“%s”,a); //puts(a);

14  #include  int main(void)  {  char text[100];  int size, i;  printf("Enter a string: ");  scanf("%s", text);  printf("you entered %s\n", text);  size = strlen( text );  printf("The number of characters in the input string is %d.\n", size);  return 0;  }  The strlen function returns the number of characters in a string, excluding „\0‟.

15  Now if we access all characters of a string in a loop, we have two approaches to terminate the loop:  (1) the length of the string ---- strlen()  (2) ‘\0’

16  Print out the reversed string  #include  int main(void)  {  char text[100];  int size, i;  printf("Enter a string: ");  scanf("%s", text);  printf("you entered %s\n", text);  size = strlen( text );  for(i = size - 1; i >= 0; i--)  printf("%c", text[i]);  printf("\n");  }

17  #include  int main(void)  {  char text[100];  int size, i, upper = 0, lower = 0;  printf("Enter a string: ");  scanf("%s", text);  size = strlen( text );  for(i = 0; i < size; i++)  {  if('A' <= text[i] && text[i] <= 'Z')  upper++;  else if('a' <= text[i] && text[i] <= 'z')  lower++;  } printf("upper = %d, lower = %d\n", upper, lower);  }

18  strcmp(str1, str2)  This function starts comparing the first character of each string.  If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

19  A zero value indicates that both strings are equal.  A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2;  And a value less than zero indicates the opposite.

20  #include  int main(void)  {  char str1[] = "cat", str2[] = "car";  printf("%d", strcmp(str1, str2));  return 0;  }


Download ppt "Dr. Sajib Datta Feb 21, 2014.  In the last class we discussed: ◦ Bubble sort  How it works  performance."

Similar presentations


Ads by Google