1 / 5

Program for Word Replacement, Sorting Names, and Space Normalization in C

This collection of C programming examples includes: a program to replace multiple spaces with a single space, a program that capitalizes the first letter of each word except for the word "and", and a program that substitutes a specific word with another word within a given sentence. Additionally, it features a name sorting program that reads five names and prints them in alphabetical order. These examples cover various string manipulation techniques and utilize standard C libraries like `<stdio.h>` and `<string.h>`.

lazar
Télécharger la présentation

Program for Word Replacement, Sorting Names, and Space Normalization in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Examples

  2. Example • Write a program that replaces multiple spaces with one space. • while(str[i] != '\0') { • if(str[i] == ' ') { • spc++; • if(spc == 1) putchar(str[i]); • } • else { • spc = 0; • putchar(str[i]); • } • i++; • } • printf("\n"); • return(0); • } • #include <stdio.h> • int main(void) { • char str[80]; • inti=0, spc=0; • gets(str); CENG 114

  3. Example • Write a program that capitalizes the first letter of each word except “and”. • #include <stdio.h> • #include <string.h> • #include <ctype.h> • int main(void) { • char str[80]; • char *wrd; • char *delim = " "; • gets(str); • wrd = strtok(str, delim); • while(wrd!= NULL) { • if(islower(wrd[0]) && strcmp(wrd, "and") != 0) • wrd[0] = toupper(wrd[0]); • printf("%s ", wrd); • wrd = strtok(NULL, delim); • } • printf("\n"); • return(0); • } CENG 114

  4. Example • Write a program substitutes a word with another word in a given sentence • #include <stdio.h> • #include <string.h> • int main(void) { • char str1[80]; • char str2[80]; • char pair[2][10] = {"display", "show"}; • char *stat; • gets(str1); • strcpy(str2, ""); • stat = strtok(str1, " "); • while(stat != NULL) { • if(strcmp(stat, pair[0]) == 0) • strcat(str2, pair[1]); • else • strcat(str2, stat); • strcat(str2, " "); • stat = strtok(NULL, " "); • } • puts(str2); • return(0); • } CENG 114

  5. Example • Write a program which reads 5 names, sorts them, then prints the sorted names • #include <stdio.h> • #include <string.h> • int main(void) { • char Names[5][15]; • char Temp[15]; • inti, j; • for(i=0; i<5; i++) { • printf("Enter name %d: ", i); • scanf("%s", Names[i]); • } • for(i=0; i<4; i++) { • for(j=0; j<4-i; j++) { • if(strcmp(Names[j], Names[j+1]) > 0) { • strcpy(Temp, Names[j]); • strcpy(Names[j], Names[j+1]); • strcpy(Names[j+1], Temp); • } • } • } • for(i=0; i<5; i++) puts(Names[i]); • return(0); • } CENG 114

More Related