C++ :: Finding A String Within Array?
Jan 14, 2013
i just want to check to see if one word (string) or one sentance is equal to any others within an array. My code
so basically i want to check if a is equal to either "sleep", "in", or "bed". Or even if the string within a is equal to "sleep in bed" as one string.
I have used the following but even if i get it correct and it says correct it says incorrect another 2 times because it is saying yeah you have gotten one right out of the possible 3. Even if I type "sleep in bed" it still does it (prints out incorrect another 2 times. Also are there any good books to start off with c++?
string a;
string aa[3] = {"sleep", "in", "bed"};
for(int i = 0; i < 3; i++) {
if(a == aa[i]) {
cout << "CORRECT!" << endl;
} else {
cout << "INCORRECT!" << endl;
}
}
View 18 Replies
ADVERTISEMENT
Sep 8, 2014
I need to find a string with leading spaces like " target sting" inside another sting.
And I need to find something like "target sting" inside another sting.
I used .IndexOf() but noticed it ignores leading spaces.
So then I went with .Substring() but that doesn't seem like that's the best solution either.
View 5 Replies
View Related
Mar 6, 2015
I have a question about this function.
Code:
char a[4] = {"aaa"};
strstr(a, "bb");
When I do this after function copies bb to the array it puts '' ? I mean last for array would be 'b' 'b' 'a' '' or 'b' 'b' '' ''. I am trying to learn the basics of searching a string, finding and changing them with another string.
View 14 Replies
View Related
Mar 2, 2014
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Program Goal
//Have 3 Strings to represent 3 categories
//Have the user type in their name
//Have the user type in a full sentence how they are feeling
//Program should find the word in the user inputted sentence and display an appropriate response
[Code]...
View 4 Replies
View Related
Oct 24, 2013
I am trying to take a string that is within the main function, and write a void function that gives me the most common alpha character used inside the string. How to mix a string and an array together like that as I am not too familiar with arrays yet.
View 8 Replies
View Related
Apr 5, 2013
I want to find that whether the 2d Vector table having duplicate or not. I can see lot of programs for removing duplicates by using unique STL algorithm. Which is the best way to find " is Duplicate or not " for 100,000 Records.
View 1 Replies
View Related
May 19, 2014
I know how to find the occurrences of a character in a string, but I have a more specific problem.
For example, consider the string:
" C 1.3825 4.0000 12.0000 1.9133 0.1853 0.9000 -1.1359 4.0000 "
I want to extract a vector that contains the positions of every first character for each number.
For the example above, the output should be a vector with elements [6 15 23 33 etc...]. These are the positions of the first character for every number.
I need to be able to do this for any arbitrary string with any arbitrary amount of numbers and characters in it (I also need to account for negative numbers).
View 1 Replies
View Related
Feb 21, 2015
i couldnt solve the algorithm exactly. The program asks the user for a string. "Bugun h@v@ cok g'uzel" for example. then the program asks for another string. If that string doesnt exists in the main string program should say there isnt any substring. If it exist then the program should print the remaining part of main string. For example:
Write the first string: Tod@y weather is be'@utiful
write the substring : ug
>>ugun h@v@ cok guzel
write the substring :wldnqwbdbj
>>there isnt any substring
Here where i came so far
#include <stdio.h>
int main()
{
char mainstr[50],substr[50];
[Code]....
View 7 Replies
View Related
Mar 28, 2013
bool isUnique(string _str)
{
bool char_set[256];
int len = _str.length();
memset(char_set, '/0', 256);
for(int i = 0; i < len; ++i)
[Code] .....
I came across this code to find if string has unique characters...i didnt understand why they subracted ascii value of character '0' in the statement int val = _str[i]- '0' and what is happening with the statements...
if(char_set[val])
{
return false;
}
char_set[val] = true;
I take each character in the sting and traverse the whole string .and if count is 2 i use break and conclude that its not unique and not otherwise...can i use this method or this is not efficient????
View 8 Replies
View Related
May 17, 2014
the code shows all characters frequency, however i want to find which one has highest frequency for example cprogrammmmming cboard highest frequency: m
char string[100], ch;int c =0, count[26]={0};
printf("Enter a string");
gets(string);
[Code].....
View 8 Replies
View Related
Mar 11, 2014
i've been writing some code for an assignment and it is mostly about pointers and string manipulations. It runs but crashes and I think it might be from over- valuating some strings maybe not. I have written in check points to make sure each function passes through but quits at findFirstPath loop, I had kept running the program through as i added more and more code. It had stopped when I near finished I believed it to just be because I hadn't finished the functions I called.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXSTRING 10
#define TRUE 1
[Code]....
View 3 Replies
View Related
Apr 25, 2014
I am reading a file whick looks like following:
Code:
10 = NAND(1, 3)
11 = NAND(3, 6, 5)
15 = NAND(9, 6, 2, 8)
Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?
My code:
Code:
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))
{
std::size_t guard_found = line.find(guard_str);
[Code].....
View 8 Replies
View Related
Jul 9, 2014
I want to search a 2D array to see if it contains a certain value x. I can come up with three different ways to do this. Provided I have:
const int Rows = 40;
const int Columns = 30;
int SampleData[Rows][Columns] = { ... }
Is there any real difference (in terms of performance etc.) between these, or are there an even better solution?
Solution 1:
for(unsigned row = 0; row < Rows; ++row) {
for(unsigned col = 0; col < Columns; ++col) {
if(SampleData[row][col] == x) {
// found
} } }
Solution 2:
int* data = &SampleData[0][0];
if(find(data, data + Rows * Columns, x) != data + Rows * Columns) {
// found
}
Solution 3:
int* data = &SampleData[0][0];
for(unsigned i = 0; i < Rows * Columns; ++i) {
if(*data++ == x) {
// found
} }
View 1 Replies
View Related
Dec 24, 2013
So for a project I'm working on, I'm using an array and generating it's values randomly but unique. Currently I'm working on a 3X3 array and the generated values are in the range from 1-9. So I wrote a function that will tell me the position of the cell whose value is 9. This is the function I wrote:
Code: void Llogaritje1(int t[3][3],int &i, int &j){
int y,l;
for(y=0;y<3;y++){
for(l=0;l<3;l++){
if(t[y][l]==9){
i=y;
j=l;
break;
}
}if(t[y][l]==9) break;
}
}
But it doesn't work on all cells. Seems like at cells t[1][0] and t[2][0] the values that i and j take are 0 0 since when I print them after excecuting the function that's what it returns. I really don't understand why.
View 4 Replies
View Related
Sep 9, 2013
I am trying to make my program read a bunch of numbers in an array to find its maximum and minimum. the program will ask the user to enter in as much number as possible until they enter a non number/letter. i got my program to find the maximum value but the program couldn't read the minimum value. it always says zero. also everytime i enter the number 0, the program will just finish its loop statement. If i typed in a negative number, it'll read the negative number as its minimum.
Code:
#include <stdio.h>
int main()
{
//-------variables------------------
double list[1000]; // can hold 1000 items
int i;
char letter;
int max = list[0];
int min = list[0];
[Code] ....
View 5 Replies
View Related
Apr 11, 2014
Most of this program is working correctly, however when I get to the part on lines 70 to 78 to try and use the function on lines 103 to 113 to find out each diagonals value and then print each one back. But when this runs it only returns the value for [0][0] for each run through the loop and I'm not really sure why. I know its probably something simple but I'm just missing it,
Code:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4 #define MAX 100
5
6 void display_menu();
7 int check_option(int);
[Code]...
View 4 Replies
View Related
Mar 20, 2014
I've been trying for over an hour to think of the logic to find the mode of an array.I left out code for initializing the arrays, but all the values in the array are between 0-9. Check out my code below.
Code:
int mode( int array[], int size ) {
int i;
int count;
int max = 0;
int num = 0;
int mode;
[code]....
View 7 Replies
View Related
Apr 16, 2013
I am attempting to write a program that takes a number(picked by the user) of test scores in an array then calculates the average and outputs the number of passed and failed tests.
On a side note I am attempting to use array notation in the main function and pointer notation in the other functions
#include<iostream>
#include<iomanip>
using namespace std;
int numScores;
int counter = 0;
double *scores;
//Function Prototypes
[Code]...
View 3 Replies
View Related
Feb 17, 2014
"Write a program in C that finds the element of an array which all the previous are smaller and all the next are bigger than that.If there are more than one print the biggest. If there isn't any print "ERROR" .
EXAMPLE
If the array has 10 elements like this : {2,3,4,6,5,7,8,10,9,8}
The correct answer is 7 , because 2,3,4,6,5 are smaller than 7 and 8,10,9,8 are bigger than 7.
I am working on it for 2 weeks and I can't find a working solution />/>/>
There is the first try :
#include <stdio.h>
#include <stdbool.h>
int s_data[10]={2,3,4,6,5,7,8,10,9,8};
int main() {
int result,i,j,s_len ,tmp1,tmp;
bool GT_PREV,ST_NEXT;
[Code] .....
View 11 Replies
View Related
Aug 12, 2014
after staring at this for awhile, I can't figure out why it won't work. It prints out numbers from 0 to 100, but will print out an absurdly high number as the highest number, like 790 or 640. I can't see why.
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int find_highest(int array[]);
int find_highest (int array[], int size) {
int highest_num;
for (int i = 1; i < size; ++i) {
if (array[i] > array[i-1]) {
[code]....
View 1 Replies
View Related
Feb 2, 2014
I'm supposed to find the biggest number (largest value), in any given array A with n numbers. (no floats)
My thoughts here are, check if A[0] > A[1]. if yes, check if A[0] > A[2]. If no, check if A[1] > A[2] etc.
I'm not sure how I can do this in code. I'm thinking if A[0] > A[1] is true, then set A[0] = A[k]. Kind of set it aside, and use that for the next if test. But I'm not sure how to do it.
This is my code so far.
Code:
int main(){
int A[7] = {12, 6, 9, 33, 2, 25, 53};
int i, k;
k = 0;
[Code]....
I'm aware of the flaws here, but this is the best I can do so far. How can I get the if test to use A[k] next, as A[k] will always be the biggest value?
View 11 Replies
View Related
Feb 3, 2015
How i can find two equal int in array with O(n) time complexityand O(1) place complexity?
View 8 Replies
View Related
Mar 26, 2013
I know how to find a specific number in a multidimensional array, but I don't know how to have a specific number and find its coordinates.
View 2 Replies
View Related
Sep 9, 2013
I have to use numbers in my .txt file that are stored in an array. So i have to write a function to find the mode of the numbers in my array.
View 5 Replies
View Related
Oct 17, 2014
I was assigned to create a program that has a 10 x 8 two dimensional array and fill it with random numbers in the range 50 to 70 inclusively.( Got That part down). The second part is to make function named findSmallest that will find and print, appropriately labeled, the smallest number in the array.
I cant seem to get it working. The code for finding the smallest give me some weird number
Here my Code:
//Assignment 18 Program 2
#include <iostream>
using namespace std;
[Code].....
View 2 Replies
View Related
Mar 12, 2014
Above you said to find the middle value you should divide the array by two and then display the middle value (aka the median). For example, for an array consisting of 1 2 3 4 5, 3 would be the middle value.
My question is, how/what do I write to tell the program to display the middle value???
So far, I have done enough research and written enough code to receive values from the user and sort the array in ascending order. I simply don't know where to go from here (I started C++ about 1 month ago). I don't know how to tell it to pull the middle value.
I've researched "find_if", "nth_value" and a boat load of others with no luck
Here's what I've done so far:
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
[Code]....
View 3 Replies
View Related