C++ :: Finding If String Has Unique Characters
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
Feb 9, 2015
I am new to C++ programming, writing a program for the below question:
given a string, how to find the longest substring with all unique characters.
View 1 Replies
View Related
Aug 10, 2014
I have been trying to write this program that reads a file of courses and then stores only the unique courses in an array of strings.
for some reason it crashes while reading the 2nd line.
printf within the function are just for me trying to trace the problem.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** uniqueCourses(FILE *fp, int *wordCount);
int main()
[code].....
View 5 Replies
View Related
Sep 9, 2013
I've recently been reading tutorials on arrays and their aquaintance with memory addresses. So, I completely understand how an array's name, when defined, is a constant pointer to its first element's address.
My problem, however, lies with characters, and how they are basically arrays except with a null terminator for the last index. What I've come to undestand, is that, when defining a character variable, each 'character' has a memory address it is associated with.
For example:
char name[] = {"Hello"}; // | 'H' | 'e' | 'l' | 'l' | 'o' | '/0' |
An address holds the value of 'H'.
An address holds the value of 'e'.
An address holds the value of 'l' and so on.
I have come to believe this is false, however. Mainly from a simple std::cout command.
std::cout << &name << std::endl; // attempt 1
std::cout << &name[0] << std::endl; // attempt 2
The first attempt, as I assumed, should print the address of the first element.
The second attempt, as I assumed, did not. I figured, &names[0] would print the address of the first element, which should have been the same as &names.
So, this brings me to my question, are characters formed of constant addresses, or are the address of individual characters not reachable?
View 9 Replies
View Related
Feb 3, 2014
I am looking for a way to correctly count the lines between two specified characters/strings in a file. Here's the part I need work on:
getline( file, lines );
do {
if(lines.find("character")
{
++counter;
}
} while( !lines.find("story") );
I want the code to search for the first occurence of the word "character," and start counting the lines from that line until it hit the first occurrence of the word "story."
Right now, I am only getting a counter value of 1.
View 2 Replies
View Related
Jan 27, 2015
I have been working on a project that deals with an array of characters and finding there frequencies. I was able to determine the frequencies that where greater than 1, but I need to find the HIGHEST frequency.
Here is my full code:
#include <iostream>
using namespace std;
void input(char unlisted[80], int& n);
void bubblesort(char unlisted[80], char sortedlist[80], int n);
[Code].....
As you can see this will print out all frequencies that are bigger than 1, but I only want the highest frequency. Basically I want to print out all frequencies when count is greater than 1 less than count, but because its in a loop count will always be greater than count-1 so it prints out all frequencies.
View 3 Replies
View Related
Mar 20, 2014
so my question is i want to print characters,no string just an array of characters,i do this but it s not working,maybe i have to put the '' at the end?
Code:
int main() {
int i;
char ch[5];
for(i = 0; i < 5; i++) {
scanf("%c",&ch[i]);
[Code]...
View 6 Replies
View Related
Jul 6, 2014
Im supposed to find the common characters between two string characters, assuming that the user wont input duplicate letters like ddog. When I run my code I get an output of a question mark upside down. Here is my code with comments on what each part is supposed to do
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str1[20], str2[20],remp = '