473,621 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on how to make a program to read and correct syntax

57 New Member
I am tring to write a program that will take a string of text input and correct the spacing and capitalization. I have been able to get it to except the string by using getline. I know that the easest way to do it would be to make everything lowercase then make loop to look for whitespace and make sure there is only one space between words, commas, and periods. I then would need another loop to make every letter after a period capital and the first letter of the string. I cant figure out how to do the loop to remove and whitespace. If anyone can help I would appreciate it.
Apr 5 '07 #1
2 1532
gdarian216
57 New Member
I am tring to write a program that will take a string of text input and correct the spacing and capitalization. I have been able to get it to except the string by using getline. I know that the easest way to do it would be to make everything lowercase then make loop to look for whitespace and make sure there is only one space between words, commas, and periods. I then would need another loop to make every letter after a period capital and the first letter of the string. I cant figure out how to do the loop to remove and whitespace. If anyone can help I would appreciate it.
Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. #include<cctype>
  4. using namespace std;
  5.  
  6. int main() { 
  7.  
  8.         cout << "enter sentence and hit enter";
  9.         string  s1; 
  10.  
  11.         getline(cin,s1);
  12.  
  13.         s1[0] = toupper(s1[0]);
  14.         for(int i = 1; i<s1.length(); i++)
  15.         {
  16.         s1[i] = tolower(s1[i]);
  17.         }
  18.         string s2;
  19.         int index = 0;
  20.         s2 = "";
  21.         for(int i=0; i<s1.length(); i++)
  22.         {
  23.         if (s1[i] != ' ')
  24.         s2[index] = s1[i];
  25.         index++;
  26.         }
  27.         cout << s2 << endl;
  28.  
  29. }
  30.  
this is what i have so far
Apr 6 '07 #2
gpraghuram
1,275 Recognized Expert Top Contributor
Hi,
There were few problem in the code...
This one works fine
Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. #include<cctype>
  4. using namespace std;
  5.  
  6. int main() { 
  7.  
  8.         cout << "enter sentence and hit enter"<<endl;
  9.         string  s1; 
  10.  
  11.         getline(cin,s1);
  12.         cout<<"OUT is :"<<s1<<endl;
  13.  
  14.         s1[0] = toupper(s1[0]);
  15.         for(int i = 1; i<s1.length(); i++)
  16.         {
  17.             s1[i] = tolower(s1[i]);
  18.         }
  19.         cout<<"OUT is :"<<s1<<endl;
  20.         string s2(s1.length(),' ');
  21.         //string s2;
  22.         int index = 0;
  23.         //s2 = "";
  24.         for(int i=0; i<s1.length(); i++)
  25.         {
  26.             if (s1[i] != ' ')
  27.             {
  28.                 s2[index++] = s1[i];
  29.             }
  30.             //index++;
  31.        }
  32.         cout<<"S2 is :"<<s2<< endl;
  33.  
  34. }
  35.  
Problem is if u declare a string like this
string s2("");
and then put element into the string like an array (s2[i]='s';)
then it is a array overwrite.

Thanks
Raghuram
Apr 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
13012
by: It's me | last post by:
Why do I get an "AttributeError: read" message when I do: import sys r=sys.stdin.read() ?? I've tried: r=sys.stdin.read(80)
8
411
by: Chad Cartwright | last post by:
I was coding a very basic C++ application, and all I wanted to do was read a space of memory at 0x005CE000 over and over again. There is another program running that is constantly writing to this address in memory, and I wanted to make a basic loop funtion to read that address in memory over and over until I closed the other program or my program. I don't know what I am doing wrong. Will C++ not let you do this? int* var;
3
6447
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
18
3026
by: David Buchan | last post by:
Hi guys, This may be a dumb question; I'm just getting into C language here. I wrote a program to unpack a binary file and write out the contents to a new file as a list of unsigned integers. It works on an IBM mainframe under AIX with GCC, but has trouble on Intel with GCC (DJGPP). I think it's an endian problem. The trouble is, although most values in the extracted list are correct,
5
2874
by: Joe Thompson | last post by:
Hi, I am using VC++.Net 2003 with WinForms to write a serial port application. I downloaded the newest VC++ examples from MSDN and found a project called Using the COM Port. In it, there is a class called Rs232. It has three lines of code: // These events allow the program using this class to react to Comm Port events.
14
1914
by: John Gerrard | last post by:
Hi, my name is John and this is my first posting to comp.lang.c. I am learning C. I am a high school student with interest in programming. I have a copy of "The C programming language second edition by Brian W Kernighan and Dennis M Ritchie". I am using gcc version 4.1 on SuSE 9. Here is my simple question. I want to set a matrix to contain some values in my program. I want to pass in the matrix array by address in a function. The...
73
4256
by: JoeC | last post by:
I am writing a game and I am having a challenge with my combat function. All I want to do is find out how to group pieces that are in the same space. There are two sides and all the units that are in the same space fight. I want to add up the attack factors and defending factors in the same space then figure out the odds so I can roll against an odds table. Basically each piece holds its own x and y loc. Here is what I have right...
5
3786
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): (R1) direct-declarator: identifier "(" declarator ")"
42
6779
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first." My question is that if it stops at a new line character (LF?) then how does one read a file with...
0
8213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8156
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8306
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1460
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.