473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String output functions dilemma....

OK,so I am working on this code in which I have to enter a string of length 20.After which I will enter a value of variable n resulting into output string which will start from index n upto white space or end of line.

For ex:-if I enter string as John Smith and n=0 then it should output string as John and if n=2 then ohn.

I tried to code this program in C/C++ but vain.Segmentation faults is the only result that I am getting.

Here is the code that I am working on.


Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include<string>
  3. #include<iostream>
  4. #include<ctype.h>
  5. using namespace std;
  6. int main()
  7. {
  8.     char a[20];
  9.     int i,n;
  10.     cout<<"Enter the desired string";
  11.     cin.getline(a,20);
  12.     cout<<"Enter string number\n";
  13.     cin>>n;
  14.  
  15.     if(n==0)
  16.     {
  17.         cout.write(a,20);
  18.     }
  19.     else
  20.     {
  21.  
  22.         cout<<"The required string is \n";
  23.         i=n;
  24.         while(a[i]!=' '||a[i]!='\0')
  25.         {
  26.             cout<<a[i];
  27.             i++;
  28.         }
  29.     }
  30.  
  31.     return 0;
  32. }


I am also confused between cin and cin.getline functions.Suggestions,hints are welcome.

Thanks in advance.
Cheers.
Jun 15 '10 #1
3 1406
Dheeraj Joshi
1,123 Expert 1GB
Few modification for your while loop. Is it necessary to check a[i]!=' ' in this loop? Why you want it? And you must check the index has not crossed 20(Since you have a character array of size 20).

Expand|Select|Wrap|Line Numbers
  1. while((a[i]!=' '|| a[i]!='\0') && i < 20)
  2.         {
  3.             cout<<a[i];
  4.             i++;
  5.         }
  6.  
Regards
Dheeraj Joshi
Jun 15 '10 #2
@dheerajjoshim
@dheerajjoshim

Thanks...that worked fine......Cant believe I am forgetting counters these days...
Jun 15 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
Happens in software development environment. :)

Regards
Dheeraj Joshi
Jun 15 '10 #4

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

Similar topics

5
by: Nasos Makriyiannis | last post by:
Hi, I'm new to XSL and I was wondering if there is a string-compare function available. I am using the following IF statement but it does not seem to be working: <xsl:if...
3
by: codefixer | last post by:
Hi, Does anybody know how to test string library functions ? I have already gone through "gcc-3.4.1/gcc/testsuite/gcc.c-torture/execute" I want to know if their is any other way to test the...
7
by: John A Grandy | last post by:
what are the preferred VB.NET analogues for IsNumeric() and Len() and CInt() & similar string-manipulation functions in VB6
4
by: Anoop | last post by:
Hi All I am getting two different outputs when i do an operation using string.digits and test.isdigit(). Is there any difference between the two. I have given the sample program and the output ...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
2
by: gitimaya | last post by:
hi, can anyone please help me in breaking up a string in array of strings. The delimeter is a character declared as '\001'. lets say i have a string as char d='\001' xxx'd'yyy'd'aaa i...
3
by: steezli | last post by:
Hi, Brand new to VB.NET and I'm having a problem figuring out this program. I'll try and be descritive as possible. I have to create a Windows application that contains a single top-level...
9
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,...
5
by: bodycode | last post by:
I am writing a dietary application usng Visual C# 2010. I need to output a decimal value that can be truncated to any number of decimal places I want, without rounding at all, on any level. I want...
2
by: disent | last post by:
Hello, I should tell you I don't know much about C++ before i get started. Ok, what I need is a way to replace some letters between the marks i choose. For example: string str1 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...

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.