The following example scans five sentences from the user and prints them

The following example scans five sentences from the

This preview shows page 4 - 6 out of 9 pages.

The following example scans five sentences from the user and prints them. #include <stdio.h> #include <string.h> #define NUM_LINES 5 #define LINE_LEN 81 int main(void) { char lines[NUM_LINES][LINE_LEN]; int i; printf("Please entert 5 sentences (not exceeding 80 characters)\n"); for ( i = 0 ; i < NUM_LINES; ++i ) { printf ("Enter the sentence number %d: ", i+1 ) ; gets (lines[i]) ; } printf("\nYou typed: \n"); for (i = 0; i< NUM_LINES; i++) puts (lines[i]); system("pause"); return 0; } Note that there is no need to  use nested loop to process an  array of strings
Image of page 4
5 Example /* This program creates an array of strings and performs various string operations on them*/ #include <stdio.h> #include <string.h> #define NUM 10 #define LEN 20 int main(void ){ int i = 0, found = 0; char str[LEN]; char names [NUM][LEN] = {"Abdullah", "Ibrahim", "Husam", "Ahmad", "Hamzah", "Abdul-aziz", "Abdul-majeed", "Nayef", "Akhaled", "Yousef"}; printf("The length of the names are:\n"); for(i = 0; i < NUM; i++){ printf("%-4d%-12s of length %d\n", i+1,names[i], strlen(names[i])); } printf("\nEnter a name to search in the list: "); gets(str); i = 0; while (!found && i<NUM) { if(strcmp(names[i], str) == 0) found = 1; else i++; }
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 9 pages?

  • Spring '10
  • baleh

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes