im trying to write a program for class that when it reads it, it corrects all the spacing and capitalization. so for example, the phrase "the quick bRown fox jumped over the lazy dog" would be converted by the program to "The quick brown fox jumped over the lazy dog"
here is my code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s;
string a; // Output string of char[].
string::size_type i=0;
string::size_type j;
string::size_type k=0;
string::size_type slen; // string length.
bool cpy_space = true; // Indicator used to copy only one space
// when deleting multiple spaces.
cout << "Enter a string: ";
getline(cin,s);
// Convert any upper case char to lower case char of s.
for(j=0;j<s.length();j++)
{
if(isupper(s[j]))
{
s[j] = tolower(s[j]);
}
}
// Delete leading spaces in s if any.
while (isspace(s[i]))
{
i++;
}
// slen is the string length after deleting input string
// leading spaces.
slen = s.length() - i;
// The first char is always upper case, and copy it to a[].
s[i] = toupper(s[i]);
a[k] = s[i];
k++;
i++;
// Process rest of the string, copy s[] to a[] one char at a time.
//while(!isspace(s[i]))
while(s[i] != '.')
{
// Still need to check and delete multiple spaces in s.
while((isspace(s[i])) && (i<slen))
// while((s[i] == ' ') && (i<slen))
{
if(cpy_space)
{
// Need to copy only one space.
a[k] = s[i];
k++;
cpy_space = false;
}
i++;
}
a[k] = s[i];
k++;
i++;
cpy_space = true; // Set cpy_space to true to use it
// next time.
}
// Almost done now. Copy '.' and eod of string '\0?.
a[k] = '.';
k++;
a[k] = '\0';
cout << "\nString after manipulation: " << a << "\n";
return 0;
}
i click run in xcode, but something goes wrong. the build succeeds but in the output screen it says (11db)
#include <iostream>
#include <string>
usingnamespace std;
int main(void)
{
string s;
string a; // Output string of char[].
string::size_type i=0;
string::size_type j;
string::size_type k=0;
string::size_type slen; // string length.
bool cpy_space = true; // Indicator used to copy only one space
// when deleting multiple spaces.
cout << "Enter a string: ";
getline(cin,s);
// Convert any upper case char to lower case char of s.
for(j=0;j<s.length();j++)
{
if(isupper(s[j]))
{
s[j] = tolower(s[j]);
}
}
// Delete leading spaces in s if any.
while (isspace(s[i]))
{
i++;
}
// slen is the string length after deleting input string
// leading spaces.
slen = s.length() - i;
// The first char is always upper case, and copy it to a[].
s[i] = toupper(s[i]);
a[k] = s[i];
k++;
i++;
// Process rest of the string, copy s[] to a[] one char at a time.
//while(!isspace(s[i]))
while(s[i] != '.')
{
// Still need to check and delete multiple spaces in s.
while((isspace(s[i])) && (i<slen))
// while((s[i] == ' ') && (i<slen))
{
if(cpy_space)
{
// Need to copy only one space.
a[k] = s[i];
k++;
cpy_space = false;
}
i++;
}
a[k] = s[i];
k++;
i++;
cpy_space = true; // Set cpy_space to true to use it
// next time.
}
// Almost done now. Copy '.' and eod of string '\0?.
a[k] = '.';
k++;
a[k] = '\0';
cout << "\nString after manipulation: " << a << "\n";
return 0;
}
How's that? Thanks for showing me this, been posting incorrectly all along