8.4 SIMPLE STRING PROGRAMS

8.4.1 Capitalize first character

Problem: Write a program to read a sentence (string) into an array and capitalize the first character of every word in the input.

Solution: We have to identify the first character of every word. We are going to scan the array character by character. When we encounter a blank, it means we are going to get a fresh word. This information is stored in a variable flag. To get uppercase character we use function toupper(ch) defined in<ctype.h>. See Program 8.2.

Program 8.2 Capitalize first character

// cap1.cpp#include<stdio.h>#include<iostream.h>#include<string.h>#include<ctype.h>int main(){ int i ,flag ;  char s1[20], s2[20] ;  cout << “<---cap1.cpp--->” << endl ;  cout << “ give string ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.