Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
#include<iostream>
#include<cctype>
using namespace std;
char C[56] , cF[16], cM[16], cL[24];
void combine (char a[] ,char b[] ,char c[]);

void main ()
{
    int initial=0, final, j, index=0;
    cout<<"Enter your complete name seperated by spaces \n";
    cin.get(C , 55);
    final= initial;
    while (C[final]!= ' ')
    {
        final++;
    }
    for (j=initial; j<final ; j++)
    {
        cF[index] = C[j];
        index++;
    }
    initial= final + 1;
    index=0;

    final= initial;
    while (C[final]!= ' ')
    {
        final++;
    }
    for (j=initial; j<final ; j++)
    {
        cM[index] = C[j];
        index++;
    }
    initial= final + 1;
    index=0;

    final= initial;
    while (C[final]!= ' ')
    {
        final++;
    }
    for (j=initial; j<final ; j++)
    {
        cL[index] = C[j];
        index++;
    }
    combine (cF,cM ,cL);


}

void combine (char a[] , char b[] , char c[])
{
    int i;
    a[0]= toupper (a[0]);
    b[0]= toupper (b[0]);
    c[0]= toupper (c[0]);
    cout<<a[0]<<". "<<b[0]<<". ";
    for (i=0 ; i<strlen(c); i++)
        cout<<c[i];
    cout<<endl<<endl;
}
Posted
Updated 1-May-10 21:58pm
v4

1 solution

First of all a few problems:
1. For strlen to work, you should include string.h or cstring depending on your compiler.
2. Your method declaration for combine should match at both places (return type of function should be void at last).

Now, the actually error, we call it Copy-Paste Error. In the last final loop you are iterating your array till you find a space, which is not necessary in each case. Rather you should look for a End of String '\0'. So change your last while loop to:
C
while (C[final]!= '\0')
{
    final++;
}


It works for me after doing this.
If input is: Manoj Kumar Singh
Output is: M. K. Singh

I hope this is what you expected.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900