get turkish chars from string - c++

i wish to write a string in reverse order , i get string with cin and iterating it via for loop from strings length to 0. Problem is when i take turkish char it writes wrongly and also 1 turkish char increases strings length by 2 (i.e. ömür has length 6)
string text = "ömür";
for ( int i = text.length() ; i >= 0; i--)
{
if(!isspace(text[i]) && text[i] != '\0')
{
cout<<text[i];
}
}
expected output = rümö =>
what i get = r??m??

The problem is that nowdays, non ASCII characters take up more than one byte (C++ char). Your best bet is to use a library such as ICU that will sort out the Unicode stuff for you. You could then do:
#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <iostream>
int
main(int argc, char **argv)
{
icu::UnicodeString text("ömür");
text.reverse();
std::cout << text;
}

Related

How to loop through certain Ascii characters in c++

I only want to loop through certain Ascii characters and not all are directly next to each other . For example I only want to loop from char '1 to 7' and then from char '? to F'. I dont want to loop through '8 to >' . I have this for loop but this will include the char I don't want.
for (char i = '1'; i < 'H'; i++)
How should I modify it to only loop through what I want?
Looping from 1 to 7 is straight-forward, since the arabic numerals ('0' to '9') are required to continguous and increasing values by all C and C++ standards.
for (char c = '1'; c <= '7'; ++c)
or (a more common style)
for (char c = '1'; c < '8'; ++c)
The problem with trying to loop through your second set of ASCII characters ('?' to 'F') is that there are character sets other than ASCII - in which the order of characters is different. For example, in ASCII, '?' is one less than '#', but that is not guaranteed for other character sets. Instead, create a string with the characters you want to loop over, and iterate over the string. For example;
const std::string characterset = "?#ABCDEF";
for (char c : characterset) // option 1, C++11 and later
{
// do something with c
}
for (auto c : characterset) // option 2, C++11 and later (type deduction)
{
// do something with c
}
// Option 3 (all C++ standards)
for (std::string::const_iterator it = characterset.begin(), end = characterset.end();
it != end; ++it)
{
char c = *it;
// do something with *it or c (it is an iterator that references a character)
}
will loop over your second set of characters.
If you want to do it all in a single loop, then change the character set. For example, a modified version of Option 1 above might be;
const std::string characterset = "1234567?#ABCDEF";
This is a more general approach that doesn't rely on your implementation (host system, compiler, library) supporting the ASCII character set (or compatible).
Each character has a fixed ASCII value associated with it. You can refer to any character with that particular ASCII value. You can just skip the characters you do not want with an 'if' condition. You will find all the ASCII values here. Referring to your example, if you want to skip the characters from '?' to 'F', the code might look something like this:
#include <iostream>
using namespace std;
int main()
{
for (char i = '1'; i < 'H'; i++)
{
if(i>=63 && i<=70)
// 63 is the ASCII value for '?'
// 70 is the ASCII value for 'F'
{
// skipping the ASCII values we do not need
continue;
}
cout << i << "";
}
return 0;
}
Create a set containing the characters you want to loop over and loop over that set.
For example :
#include <iostream>
#include <stdexcept>
#include <string>
#include <set>
// character_set.h
//-------------------------------------------------------------------------
// To be able to easily input a character range we need a helper struct
struct character_range_t
{
// have this destructor so a character range can be used in brace initialization.
character_range_t(const char f, const char t) :
from(f),
to(t)
{
if (to < from) throw std::invalid_argument("to must be larger or equal to from");
}
char from;
char to;
};
//-------------------------------------------------------------------------
// helper function to combine multiple character ranges into on set
// input is a compile time array of ranges
template<std::size_t N>
auto make_character_set(const character_range_t(&ranges)[N])
{
// I chose a set because all elements must be unique and set does that.
std::set<char> set;
// loop over all input ranges
for (std::size_t n = 0; n < N; ++n)
{
// and for each range add the characters in the range to the set
for (char c = ranges[n].from; c <= ranges[n].to; ++c) set.insert(c);
}
return set;
}
// main.cpp
//-------------------------------------------------------------------------
// #include "character_set.h"
int main()
{
auto set = make_character_set({{'1','7'},{'?','F'}});
// use range based for loop to loop over all characters in the set
for (const char c : set)
{
std::cout << c << " ";
}
}

C++ - Capitalize the first Character for each word in the given string

I have a char pointer , then i convert it into string format. So i would like to capitalize the first character of each word in this string, I wrote the code below :
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
void transform(char *s);
int main(int argc, char const *argv[])
{
char *str = new char[255];
strcpy(str, "jimmy catter");
string s;
s+=str; //convert char to string
std::cout<<s<<endl;
for (int i = 0; i < s.length(); ++i)
{
if (s[i] == ' ')
{
if (islower(s[i+1])==1)
{
s[i+1] = toupper(s[i+1]);
}
}
}
std::cout<<s<<endl;
return 0;
}
I would like to the output shoule be "Jimmy Catter" , but the result i got is still in the lower format. Could you please help look with this ?
So basically, there are two things here:
islower() should be treated as if it returns a bool, even if it says it returns an int. This is because for a bool, the actual value is false if 0 and true if any value other than 0. This means that if you wanted to still compare to a numerical value you would say islower(s[i]) != 0 since 1 is not the only valid internal value for true.
For the first character (J), there isn't a space before it. So, what we can do is check the current character is the first character OR if the previous character is space. Basically, instead of checking the current character and changing the next character, we check the previous character (or if it's the first character) and then change the current character.
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
void transform(char *s);
int main(int argc, char const *argv[])
{
char *str = new char[255];
strcpy(str, "jimmy catter");
string s;
s+=str; //convert char to string
std::cout<<s<<endl;
for (int i = 0; i < s.length(); ++i) {
if (i == 0 || s[i - 1] == ' ') {
if (islower(s[i])) {
s[i] = toupper(s[i]);
}
}
}
std::cout<<s<<endl;
return 0;
}
First thing to note here is that islower returns non zero(>0) number if argument is lowercase instead of 1.
Second you are trying to capitalize char by checking if char before it was whitespace. Nothing wrong with this approach but it won't work on first character so you can do it by adding an extra line before the loop. So with some tweaking it works fine now:
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
// void transform(char *s);
int main(int argc, char const *argv[])
{
string s = "jimmy catter ";
/*
char *str = new char[255];
strcpy(str, "jimmy catter");
string s;
s+=str; //convert char to string
*/
std::cout<<s<<endl;
s[0]=toupper(s[0]);
for (int i = 0; i < s.length()-1; ++i)
{
if (s[i] == ' ')
{
if (islower(s[i+1])>=1)
{
s[i+1] = toupper(s[i+1]);
}
}
}
std::cout<<s<<endl;
return 0;
}
You may use ASCII notations because they are quite clear to understand and execute in such programs.
I modified your code a little bit in the loop part and it gives correct output. Have a look at it.
for (int i = 0; i < s.length(); ++i)
{
if(!i&&97<=s[i]<=122) //handles border condition where first letter is small case
{
s[i] = s[i]-32;
}
if ((i!=s.length-1)&&s[i] == ' ') //prevents invalid output where last character is a space
{
if (97<=s[i+1]<=122) //checks if letter after space is small case
{
s[i+1] = s[i+1]-32;
}
}
}
std::cout<<s<<endl;
return 0;
}

Scanning ASCII value of each character of a string

Is there anyway , if I enter any string , then I want to scan ASCII value of each character inside that string , if I enter "john" then I should get 4 variables getting ASCII value of each character, in C or C++
Given a string in C:
char s[] = "john";
or in C++:
std::string s = "john";
s[0] gives the numeric value of the first character, s[1] the second an so on.
If your computer uses an ASCII representation of characters (which it does, unless it's something very unusual), then these values are the ASCII codes. You can display these values numerically:
printf("%d", s[0]); // in C
std::cout << static_cast<int>(s[0]); // in C++
Being an integer type (char), you can also assign these values to variables and perform arithmetic on them, if that's what you want.
I'm not quite sure what you mean by "scan". If you're asking how to iterate over the string to process each character in turn, then in C it's:
for (char const * p = s; *p; ++p) {
// Do something with the character value *p
}
and in (modern) C++:
for (char c : s) {
// Do something with the character value c
}
If you're asking how to read the string as a line of input from the terminal, then in C it's
char s[SOME_SIZE_YOU_HOPE_IS_LARGE_ENOUGH];
fgets(s, sizeof s, stdin);
and in C++ it's
std::string s;
std::cin >> s; // if you want a single word
std::getline(std::cin, s); // if you want a whole line
If you mean something else by "scan", then please clarify.
You can simply get the ascii value of a char by casting it to type int:
char c = 'b';
int i = c; //i contains ascii value of char 'b'
Thus, in your example the code to get the ascii values of a string would look something like this:
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
int main()
{
string text = "John";
for (int i = 0; i < text.size(); i++)
{
cout << (int)text[i] << endl; //prints corresponding ascii values (one per line)
}
}
To get the corresponding char from an integer representing an entry in the ascii table, you just have to cast the int back to char again:
char c = (char)74 // c contains 'J'
The code given above was written in C++ but it basically works the same way in C (and many other languages as well I guess)
There is no way to turn a string of length 'x' into x variables. In C or C++ you can only declare a fixed number of variables. But probably you don't need to do what you are saying. Perhaps you just need an array, or most likely you just need a better way to solve whatever problem you are trying to solve. If you explain what the problem is in the first place, then I'm sure a better way can be explained.
Ya,I think there are some more better solutions are also available but this one also be helpful.
In C
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(){
char s[]="abc";
int cnt=0;
while(1){
if(s[cnt++]==NULL)break;
}
int *a=(int *)malloc(sizeof(int)*cnt);
for(int i=0;i<cnt;i++)a[i]=s[i];
for(int i=0;i<cnt-1;i++)printf("%d\n",a[i]);
return 0;
}
In C++
#include <iostream>
#include <string>
using namespace std;
int main(){
string s="abc";
//int *a=new int[s.length()];
//for(int i=0;i<s.length();i++)a[i]=s[i];
for(int i=0;i<s.length();i++)
cout<<(int)s[i]<<endl;
return 0;
}
I hope this one will be helpful..
yeah it's very easy ..just a demo
int main()
{
char *s="hello";
while(*s!='\0')
{
printf("%c --> %d\n",*s,*s);
s++;
}
return 0;
}
But make sure your machine is supporting the ASCII value format.
In C every char has one integral value associted with it called ASCII.
Using %d format specifier you can directly print the ASCII of any char as above.
NOTE: It's better to get good book and practice this kind of program yourself.

How to find substring from string?

How do I find a substring from the string path "/user/desktop/abc/post/" using C/C++? I want to check if folder "abc" is present or not in that path.
Path is character pointer char *ptr = "/user/desktop/abc/post/";
Use std::string and find.
std::string str = "/user/desktop/abc/post/";
bool exists = str.find("/abc/") != std::string::npos;
In C, use the strstr() standard library function:
const char *str = "/user/desktop/abc/post/";
const int exists = strstr(str, "/abc/") != NULL;
Take care to not accidentally find a too-short substring (this is what the starting and ending slashes are for).
Example using std::string find method:
#include <iostream>
#include <string>
int main (){
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
size_t found = str.find(str2);
if(found!=std::string::npos){
std::cout << "first 'needle' found at: " << found << '\n';
}
return 0;
}
Result:
first 'needle' found at: 14.
Use strstr(const char *s , const char *t)
and include<string.h>
You can write your own function which behaves same as strstr and you can modify according to your requirement also
char * str_str(const char *s, const char *t)
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++)
{
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++);
if (k > 0 && t[k] == '\0')
return (&s[i]);
}
return NULL;
}
As user1511510 has identified, there's an unusual case when abc is at the end of the file name. We need to look for either /abc/ or /abc followed by a string-terminator '\0'. A naive way to do this would be to check if either /abc/ or /abc\0 are substrings:
#include <stdio.h>
#include <string.h>
int main() {
const char *str = "/user/desktop/abc";
const int exists = strstr(str, "/abc/") || strstr(str, "/abc\0");
printf("%d\n",exists);
return 0;
}
but exists will be 1 even if abc is not followed by a null-terminator. This is because the string literal "/abc\0" is equivalent to "/abc". A better approach is to test if /abc is a substring, and then see if the character after this substring (indexed using the pointer returned by strstr()) is either a / or a '\0':
#include <stdio.h>
#include <string.h>
int main() {
const char *str = "/user/desktop/abc", *substr;
const int exists = (substr = strstr(str, "/abc")) && (substr[4] == '\0' || substr[4] == '/');
printf("%d\n",exists);
return 0;
}
This should work in all cases.
If you are utilizing arrays too much then you should include cstring.h because it has too many functions including finding substrings.

Reversing a string, weird output c++

Okay, so I'm trying to reverse a C style string in C++ , and I'm coming upon some weird output. Perhaps someone can shed some light?
Here is my code:
int main(){
char str[] = "string";
int strSize = sizeof(str)/sizeof(char);
char str2[strSize];
int n = strSize-1;
int i =0;
while (&str+n >= &str){
str2[i] = *(str+n);
n--;
i++;
}
int str2size = sizeof(str)/sizeof(char);
int x;
for(x=0;x<str2size;x++){
cout << str2[x];
}
}
The basic idea here is just making a pointer point to the end of the string, and then reading it in backwards into a new array using pointer arithmetic.
In this particular case, I get an output of: " gnirts"
There is an annoying space at the beginning of any output which I'm assuming is the null character? But when I try to get rid of it by decrementing the strSize variable to exclude it, I end up with some other character on the opposite end of the string probably from another memory block.
Any ideas on how to avoid this? PS: (would you guys consider this a good idea of reversing a string?)
A valid string should be terminated by a null character. So you need to keep the null character in its original position (at the end of the string) and only reverse the non-null characters. So you would have something like this:
str2[strSize - 1] = str[strSize - 1]; // Copy the null at the end of the string
int n = strSize - 2; // Start from the penultimate character
There is an algorithm in the Standard Library to reverse a sequence. Why reinvent the wheel?
#include <algorithm>
#include <cstring>
#include <iostream>
int main()
{
char str[] = "string";
std::reverse(str, str + strlen(str)); // use the Standard Library
std::cout << str << '\n';
}
#ildjarn and #Blastfurnace have already given good ideas, but I think I'd take it a step further and use the iterators to construct the reversed string:
std::string input("string");
std::string reversed(input.rbegin(), input.rend());
std::cout << reversed;
I would let the C++ standard library do more of the work...
#include <cstddef>
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
typedef std::reverse_iterator<char const*> riter_t;
char const str[] = "string";
std::size_t const strSize = sizeof(str);
char str2[strSize] = { };
std::copy(riter_t(str + strSize - 1), riter_t(str), str2);
std::cout << str2 << '\n';
}
while (&str+n >= &str){
This is nonsense, you want simply
while (n >= 0) {
and
str2[i] = *(str+n);
should be the much more readable
str2[i] = str[n];
Your while loop condition (&str+n >= &str) is equivalent to (n >= 0).
Your *(str+n) is equivalent to str[n] and I prefer the latter.
As HappyPixel said, your should start n at strSize-2, so the first character copied will be the last actual character of str, not the null termination character of str.
Then after you have copied all the regular characters in the loop, you need to add a null termination character at the end of the str2 using str2[strSize-1] = 0;.
Here is fixed, working code that outputs "gnirts":
#include <iostream>
using namespace std;
int main(int argc, char **argv){
char str[] = "string";
int strSize = sizeof(str)/sizeof(char);
char str2[strSize];
int n = strSize-2; // Start at last non-null character
int i = 0;
while (n >= 0){
str2[i] = str[n];
n--;
i++;
}
str2[strSize-1] = 0; // Add the null terminator.
int str2size = sizeof(str)/sizeof(char);
int x;
cout << str2;
}