C :: Swapping Some Numbers In String

Jun 15, 2013

I have a pointer to a pointer to a C string:

char**objectData;

In this string there are some numbers with a dash between them, for example, "5-10". I need to read these numbers in and then increment them. So "5-10" becomes "6-11", "6-11" becomes "7-12", etc.

So far I have:

Code: char temp[350]; //350 chars is plenty enough

// Copy the first characters BEFORE the numbers into a new char array. The next characters are the numbers.
strncpy(temp, *objectData, 39); The next steps are:
- Extract the two numbers from the C string (determining if the number is 0,2 or 3 digits long) and write them into two ints. I'm stuck here.

- Increment the ints

- Write the ints into the array with a dash inbetween.

View 6 Replies


ADVERTISEMENT

C++ :: User Defined Function - String Related Program (swapping)

Jan 4, 2015

Write a program using user-defined function which is passed a string and that function should cycle the string.(Do not use any string related functions). E.g.

If the string is : Chetna then it should print as Chetna, hetnaC, etnaCh, tnaChe,naChet, aChetn

Ans.

#include <iostream>
using namespace std;
char swamp(char a[], int x) {
for(int k=0; k<=x; k++) {
char l= a[x]; a[x]= a[0]; char p=a[x-1]; a[x-1]=l;
for(int i=1; i<x-2; i++) {

[Code]...

View 19 Replies View Related

C :: Finding Written Numbers In A String And Converting Them To Decimal Numbers

Jun 20, 2013

User enters sentence "The Smiths have two daughters, three sons, two cats and one dog." (The numbers may change depending on what the user chooses to enter. He told us the range would be from zero to nine.) and we have to convert the written numbers within the sentence into actual decimal numbers and print out the new sentence. Ex. The Smiths have 2 daughters, 3 sons...etc.

I have written the following bit of code which reads the string and finds all the "written numbers" but I am not sure how to proceed from there. I am stuck on how to print out the new sentence with the converted numbers as my professor mentioned something about creating the new string using dynamic memory allocation.

Code:
#include <stdio.h>#include <string.h>
int main () {
char A[100];
int length = 0;
int i;

[Code] .....

View 7 Replies View Related

C :: Swapping Like Function

Feb 15, 2015

I have a question about one function in my program. Write a function that will replace players in a tim. New player gets in the game, and takes the place of one that leaves. Prototype of function is:

Code:

void replace(TEAM *p,PLAYER newplayer,int num)

where second parameter is new player, and the third is a jersey number of player who leaves the game.Two structures are defined as:

Code:

typedef struct{ char name[25],surname[25];int number;}PLAYER;typedef struct{ char nameofteam[25];int numberofplayers;PLAYER *players;}TEAM;

First I tried to read which player should get out, but that didnt work:

Code:

printf("which player should get out?
"); do { scanf("%s",p->players.number) } while(p->players.number);//

Choose one of previously read players Second thing is to read a new player and replace him with the chosen who leaves.

View 2 Replies View Related

C++ :: Swapping Elements In Array?

Nov 25, 2014

I thought I'm done doing mg activity, but my professor said that we need to use a Temporary Variable for Swapping but where to put it.

Here is his activity:

Activity: Swapping Create a program that accepts a 10-element array of type int. Where the 1st user-input is stored in the 1st element of the array; the 2nd user-input is stored in the 2nd element of the array; so on so forth until the last user-input stored in the last element of the array. Your source code should be able to SWAP the values of the 1st and 10th; 2nd and 9th; 3rd and 8th; 4th and 7th; and 5th and 6th elements. It should display the values of the original and the swapped values of the array. example:

Enter 10 integer values:
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4

[Code]....

View 1 Replies View Related

C++ :: Swapping Letters In Array

May 3, 2013

I have a function that is suppose to swap positions of 2 letters but It doesn't seem to work. Im passing in the array of char into the function.

void swapletter(char word[]) {
char temp1;
int swap1;
int swap2;
cout<<"What is the first location: ";
cin>>swap1;

[Code] ....

View 6 Replies View Related

C/C++ :: Access The Swapping Function?

Nov 22, 2014

Giving the following program, how do i access the swapping function. I've tried swapp::change <int> ( a, b ) ; and it gave me 4 errors. Here's the code:

#include <iostream>
using namespace std;
template <typename T>
class swapp

[Code].....

View 5 Replies View Related

C :: Swapping Structures Inside File

Apr 9, 2013

I tried normal swapping method like this (counter is number of structures written in file) :

Code:
fopen("books.txt","r+");
system ("cls");
for(i=1;i<counter-1;i++){
for(j=1+1;j<counter;j++){
fscanf(f," %[^,], %[^,], %[^,],

[Code] ....

But it doesn't do anything.

View 2 Replies View Related

C# :: Swapping Multi Usercontrol In 1 Winform?

Jul 29, 2014

So basically, I started out with each wizard == 1 winform, but then I found another way to do it by making the content of each wizard step a user control, then say, on initial deployment, it load usercontrol1, then when i click next, the panel hide usercontrol1 for usercontrol2 and so forth. Would it be feasible to create all usercontrols (all the wizard step) and add them in an array, then i can load them by index?

View 7 Replies View Related

C/C++ :: Swapping Of Position Of Elements In A Sequence

Oct 24, 2013

Write a program that gets a sequence of unsigned integers.the user can enter at most 100 integers.

After getting the numbers, the program allows the user to repeatedly choose one of the three options:

1. swap the location of two entries in the sequence. if this option is chosen the user is prompted to enter the two locations to be swapped.

2. print out the sequence.

3. repeatedly swap two locations in the sequence until getting back to the state before this operation started. then print out the number of swaps performed.

View 3 Replies View Related

C++ :: Swapping Two Adjacent Linked Lists Nodes

Oct 22, 2014

I am working on a program where I sort elements into alphabetical order and then when one is less than the other I swap them. I first did it by swapping the data but they want me to swap the nodes instead and I am having trouble doing that.

Node *add_node( Node *list, const string &s ) {
struct Node *n = new struct Node;
n->word = s; // copy string s to word
n->next = 0;

// add node n to the list
// the list should always be in ascending alphabetical order
n->next = list;
list = n;

[Code] ....

View 2 Replies View Related

C++ :: Swapping Nodes In Doubly Linked List?

Nov 15, 2014

I've been working on this linked list priority queue . I know that the root of the problem is in my swapUp() function (swapping the positioning of two nodes based on their priority), because the list works great up until it is called. The seg fault is not actually being caused by swapUp(), it's being caused by peekAt(), which returns the element in the node at position n. But the error does not occur unless swapUp() is called first, so that is where the issue is (I think).

There is also a seg fault being caused in the destructor, which I believe may have the same root cause in swapUp().

PRIORITY QUEUE:

#ifndef JMF_PriorityQueue
#define JMF_PriorityQueue
#include <iostream>
#include <string>
template <typename T>
class PriorityQueue{

[Code] .....

Okay, so I've tried implementing SwapUp() in a different new way, but it's still giving me the same problem

template <typename T>
void PriorityQueue<T>::swapUp(Node * target){
Node * partner = target->next; //Partner = target next

[Code] .....

This is such an elementary logic problem I don't know why I'm having so much trouble with it.

View 4 Replies View Related

C/C++ :: Swapping Like Function - Replace Player In A Team

Feb 14, 2015

I have a question about one function in my program. Write a function that will replace players in a team. New player gets in the game, and takes the place of one that leaves. Prototype of function is:

void replace(TEAM *p,PLAYER newplayer,int num)

where second parameter is new player, and the third is a jersey number of player who leaves the game.
Two structures are defined as:

typedef struct {
char name[25],surname[25];int number;
}PLAYER;
typedef struct {
char nameofteam[25];int numberofplayers;PLAYER *players;
}TEAM;

First I tried to read which player should get out, but that didnt work:

printf("which player should get out?");
do {
scanf("%s",p->players.number)
} while(p->players.number);//Choose one of previously read players

Second thing is to read a new player and replace him with the chosen who leaves.

View 3 Replies View Related

C/C++ :: Swapping Elements In A Struct Dynamic Array

Dec 1, 2014

Let's say there is a document which stores data of exams of 3 subject. The document is in the below format:

Subject code [spc] Student code [spc] Exam score [endl]

Repeatedly, there are 100 data. E.g.

ENGL_S12 [spc] 000001 [spc] 90.5
ENGL_S12 [spc] 000005 [spc] 77.3
MATH_G22 [spc] 000502 [spc] 100
LATI_F11 [spc] 002005 [spc] 65.4
...

Now I have to write a function show_exam_descending(Data d, string subCode)
when I call show_exam_descending(d, "ENGL_S12")
the program will execute to show all the students' exam scores in ENGL_S12 in DESCENDING order...

For this to run, I have declared a struct Data:

struct Data {
string subjectCode;
int studentCode;
double examScore;
);

For the search, I have written a function before to load all the data from the document by using pointer and dynamic arrays. It works so well. What troubles me is the way to swap the elements (i.e. examScore) of different students in struct dynamic arrays. I am able to display all of them, but don't know how to swap.

View 8 Replies View Related

C++ :: How To Convert Numbers To String

Sep 26, 2014

I'm trying to get two numbers longer than long long int and add them together and multiply. I'm not sure how to convert the numbers to a string.

#ifndef BIG_INTEGER_H
#define BIG_INTEGER_H
#include "BigIntegerException.h"

class BigInteger {
private:
int biginteger[500];
int bigintegertwo[500];

[Code] .....

View 4 Replies View Related

C++ :: Extracting Numbers From A String

Dec 5, 2014

So, I have a string and I need to extract numbers from it. I've tried different things but they are not working. So, Here is what I have:

int main() {
string myString;
char *strPtr;

cout << "Enter a string to evaluate: " << endl;
getline(cin, myString);

[Code] ....

View 10 Replies View Related

C/C++ :: How To Subtract Two String Containing Numbers

Mar 29, 2014

I have a code for subtract of two char arrays(size of each is 50).but i want to write this program with string and i should be consider that size of each string may be not equals.for example we have s1=4777 and s2=55.and we should be subtract them. This is my code.

char* sub(char x[50],char y[50]) {
int i,j,temp=0;
char z[50]={'0'};
for(i=0;i<50;i++)

[Code] .....

View 9 Replies View Related

C++ :: Extracting Decimal Numbers From A String?

May 16, 2014

Extracting integers from a string is trivial, but I am unable to find a function or code that will extract decimal numbers from a string.

I have a string that looks something like this:
" Asdf 1 1.3825 4.0000 12.0000 1.9133 0.1853 0.9000 1.1359 4.0000 "

Any tips on extracting the numbers (in decimal form) and storing them in a vector?

View 5 Replies View Related

C/C++ :: Count Numbers And Characters In A String

Sep 2, 2014

I have a upcoming C++ exam. I would like to write a program to convert INDIVIDUAL digits into words.

E.g 678 = six seven eight[/size][/b]

I have the following code:

//Program to print individual digits in words
#include<iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number between 0 and 9999: ";

[Code] ....

View 7 Replies View Related

C/C++ :: Addition Of Infinite Numbers Using String

Feb 26, 2013

How exactly did u accept it as a string? i mean did u use ascii values or anything? and what was your code for keeping track of carry?

View 1 Replies View Related

C :: How To Take String Of Numbers And Turn It Into Array Of Integers

Mar 2, 2014

I know strings are essentially just arrays of characters, so what would be the easiest way to take each individual digit and put it into a separate space in an array?

ex.) *str = "90210"

array[0] = 9
array[1] = 0
array[2] = 2
array[3] = 1
array[4] = 0

All my attempts at achieving this just result in an array full of garbage numbers. What I've done is

Code:
int *array;
array = malloc(sizeof(int)*(strlen(str));
for(i=0; i<strlen(str); i++) {
array[i] = str[i]
}

also, I should mention that the string will be defined in main, and its converted into an array in a separate function.

View 2 Replies View Related

C :: Convert String Made Of Many Numbers To Integer

Jan 6, 2015

I am supposed to convert a string made of many numbers between every number spaces are between, to an integer variable for every number..

How am I supposed to get over this problem?

Example: String: " 322 52 231"

View 1 Replies View Related

C/C++ :: Read In String With Multiple Words And Numbers?

Jul 23, 2014

I am trying to read user input for recipe ingredients which must include a ingredient name, and may include a quantity and a unit. Example: Stone ground flour 2 cups or Sugar 1 Tbsp or Milk. The problem I am having is that the string gets cut off after a space when multiple words are used for the ingredient name.

cin.ignore();
string line;
getline(cin, line);
istringstream record(line);
string name;
string name2;
int quantity = 0;
string unit;
record>>name>>quantity>>unit;

View 1 Replies View Related

C/C++ :: Get Code To Sum A String Of Numbers Input From A File

Nov 5, 2014

This program gets input from a file and output to the screen and to a file. The difficulty I am having is summing the number I retrieve from the file for the individual numbers of sightings.

#include <fstream> // enables us to read and write files
#include <iostream> // for cin and cout
#include <cstdlib>
#include <string> // enables us to hold strings of characters
#include <cctype>
using namespace std;

[code].....

View 14 Replies View Related

C/C++ :: String Of Characters - Sorting Alphabets And Numbers

Apr 20, 2014

My program reads a string of characters. Prints all occurrences of letters and numbers, sorted in alphabetical and numerical order. Letters will be converted to uppercase.Other characters are filtered out, while number of white spaces are counted.

the problem is it crushes when i run the program

Here is my code

#include <iostream>
const int SIZE = 100;
using namespace std;
int main() {
char *pStr, str[SIZE] = "", newStr[SIZE] = "", ch;
int count = 0, i = 0, j;

[Code] .....

View 1 Replies View Related

C :: Turning String Of Numbers Into Array Of Individual Integers

Mar 2, 2014

In my program, I am fed a string that contains integers such as Code: *str = "45678" and my program is simply supposed to read the number in and store each given number in a separate spot in an integer array. So basically, when my program has finished running it should be stored like:

Code:

arr[0] = 4
arr[1] = 5
arr[2] = 6
arr[3] = 7
arr[4] = 8

The way I have been attempting to do this was just copying it to the array like so:

Code:

arr = malloc(sizeof(int) *(strlen(str));
for (i=0; i<strlen(str); i++) {
a->digits[i] = str[i];
}

however, this just seems to return an impossibly high garbage value when I do. I'm assuming the way I'm trying to store it is 'illegal', but I cant seem to find online a proper way to do it.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved