O'Reilly logo

Object Oriented Programming with C++, Second Edition by Sunil Patekar, Mahesh Bhave

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required