I'm trying to get the length of the string from a char array
My input is:alpha kian namikian and the output should be 5 4 8 but at the moment my output is 11 11 11 11 11 11 11 11 11 11 11 which is not what I'm trying to achieve.
int i,count;
char str[100];
cout<<"enter string\n";
for(i=0;;i++)
{
cin>>str[i];
if(cin.get()=='\n')
{
count=i+1;
break;
}
}
for(i=0;i<count;i++)
{
int len = strlen(str);
cout<<len<<"\n";
}
You have a compilation error because you're trying to fit an array of strings as a parameter to strlen. In your code, str contains all the strings, so you have to use access operator [] just like you did when you were taking strings from standard input.
int len = strlen(str) becomes int len = strlen(str[i]) and that should fix the error.
EDIT:
It looks like you can't use strlen with strings. Use length() instead.
int len = str[i].length()
EDIT #2:
Adding full code for reference with output:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i, count;
string str[100];
cout << "enter string\n";
for (i = 0;; i++)
{
cin >> str[i];
if (cin.get() == '\n')
{
count = i + 1;
break;
}
}
for (i = 0; i < count; i++)
{
int len = str[i].length();
cout << len << "\n";
}
}
Output:
enter string
alpha kian namikian
5
4
8
You can use this approach using the vector type string and exit the storing of the strings if entered string is exit
I am using a temp variable to store the string at any index and output the corresponding length.
int i;
vector<string> str;
cout << "Enter String\n";
while(cin)
{
string temp;
cin >> temp;
if(temp == "exit")
break;
else
str.push_back(temp);
}
int n= str.size();
for(i = 0; i< n; i++)
{
string temp = str[i];
int len = temp.length();
cout << len << endl;
}
Related
I have to find the length of an array of characters using a function that uses the pointer notation and, for some reason, I get 23 but the result should be 5.
Here is the code I built:
#include <iostream>
using namespace std;
int length(char *N)
{
int length = 0;
char* S = N;
for (; *S != '\0'; S++);
for (; *N != *S; N++)
{
length++;
}
return length;
}
int main()
{
char A[5];
char *N;
N = A;
cout << "please enter the charecters you desire" << endl;
for (int i = 0; i <=4; i++)
{
cin >> A[i];
}
cout<<length(N);
}
You don't need two loops.
And note that you have to leave a space in the array (the last element) to fill it by '\0'.
Hence by the two notes and if you want to use five characters excluding the '\0', your code will be as follows
#include <iostream>
size_t length(char *N){
size_t length = 0;
for (; *N != '\0'; N++){
length++;
}
return length;
}
int main()
{
char A[6];
std::cout << "please enter the characters you desire\n";
for (int i = 0; i < 5; i++){
std::cin >> A[i];
}
A[5] = '\0';
std::cout << length(A);
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string compression(const string & str){
int i = str.size();
string letters;
letters[0] = str[0];
for (int j = 0; j < i; ++j){
int count = 1;
while (str[j] == str[j+1]){
count++;
j++;
}
letters.push_back('0' + count);
letters.push_back(str[j]);
}
return letters;
}
int main(){
string input;
char c;
try {
cout << "Enter the data to be compressesed: "<< endl;
cin >> input;
for (int z = 0; z < input.length(); ++z){
c = input.at(z);
}
if (!(c >= 'a' && c <= 'z')){
throw runtime_error("error: invalid input");
}
}
catch (runtime_error& excpt){
cout << excpt.what() <<endl;
return 0;
}
cout << "The compressed data is " << compression(input) << endl;
return 0;
}
The expected output is , repeated for each run of characters. Here is the amount of times is repeated in sequence.
Some examples:
aaeeeeae = 2a4e1a1e
rr44errre = invalid input
eeeeeeeeeeeeeeeeeeeee = 21e
the code works properly only if the character is repeated consecutively 9 times or less. for values of 10 and more the input is other symbols.
For example it stays blank for 10, so if input is 'aaaaaaaaaabb',output just would be 'a2b' instead of '10a2b'. For 11 its outputs ';',
so if input is 'aaaaaaaaaaabb', output is ';a2b' for some reason.
So my question is, how do i make the pushback work for all numbers and not just from 0-9?
Thank you for your time if u've gotten to here. ^^
If you can use c++11 or newer your function compression could look like:
string compression(const string & str){
int i = str.size();
string letters;
for (int j = 0; j < i; ++j){
int count = 1;
while (str[j] == str[j+1]){
count++;
j++;
}
letters += std::to_string(count);
letters.push_back(str[j]);
}
return letters;
}
I am trying to convert a string that I have already parsed with spaces into a int array:
//example of string before parsing
arrElement = "1,2,3";
//parsing
for(int i =0; i < size; i++){
if(arrElements[i] == ','){
arrElements[i] = ' ';
}
}
//string is now "1 2 3"
//trying to convert numbers only into int
stringstream str;
int intCount = 0;
int intNum[size];
for(int i = 0; i < size; i++){
str << arrElements[i];
if(str == " ") {
}
else {
str >> intNum[intCount];
intCount++;
}
}
I am currently getting the result there are five integers reads, instead of the three in the example I made. In addition when I print out the array, I am completely different numbers:
209664128 32764 0 0 0
I sort of understand the issue, but I am new c++ so I could be wrong, and I am unsure how to resolve this issue. Any help would be greatly appreciated.
Here are some minimal modifications to make your example working.
I think you should avoid the successive calls between std::stringstream::operator>> and std::stringstream::operator<<.
//example of string before parsing
std::string arrElements = "1,2,3";
//parsing
for(int i =0; i < arrElements.size(); i++){
if(arrElements[i] == ','){
arrElements[i] = ' ';
}
}
//string is now "1 2 3"
//trying to convert numbers only into int
stringstream str(arrElements);
int intCount = 0;
static const int size = 3;
int intNum[size];
for(int i = 0; i < size; i++){
if(str == " ") {
}
else {
str >> intNum[intCount];
intCount++;
}
}
I'm trying to write a simple program which takes an array of chars, and spits it out backwards. I know there are plenty of other ways to shorten this using a library header function, but I wanted to do it using for loops just to get used to them.
#include<stdio.h>
#include<iostream>
using namespace std;
char string1[10];
int count = 0;
char stringy[10];
void enterString()
{
cout << "please enter a string: " << endl;
cin >> string1;
}
void stringCounter(const char stringLength[])
{
//initiate for loop i = 0
//if stringLength[i] does not does not equal 'i' then carry on
//increment i
for (int i = 0; stringLength[i] != '\0'; i++)
{
count++;
}
cout << "size of string is: " << count << endl;
}
void reverseString(int arraySize, char string2[])
{
int counter = 0;
for (int i = arraySize; i >= 0; string2[i--])
{
stringy[counter] = string2[i];
counter++;
}
stringy[count] = '\0';
cout << stringy << endl;
}
int main()
{
enterString();
stringCounter(string1);
reverseString(count, string1);
return 0;
}
This is the whole program. The program is failing in function reverseString. I can't work out how to successfully read the last index of the char array string2[] and copy it into the first index of char array stringy.
One, If the user enters a string more than 10 characters long then your enterString() function will access the array out of its bound, at cin>>string1. So better to use getline to make sure you don't read more than what your array can hold.
Two, with your current implementation the reverseString() function will write to the first element of the array with the null terminator character,if the arraySize<=10, and trying to display that string will not show you anything.
This:
cin >> string1;//will try to access the array out of its bound if user give more than it can hold,i.e 10 characters
...
for (int i = arraySize; i >= 0; string2[i--])
{
stringy[counter] = string2[i];//the first iteration will put the '\0' character as the first elements of stringy
counter++;
}
Should be changed to:
cin.getline(string1,10);//make sure to get not more than 10 characters,including the null terminator
.....
for (int i = arraySize-1; i >= 0; i--)
{
stringy[counter] = string2[i];
counter++;
}
There are many mistakes in your program. If this is the exact code you are compiling then it should throw many errors.
Following might help.
#include<iostream>
using namespace std;
void reverseString(int , char *);
int stringCounter(const char );
int stringCounter(const char stringLength[])
{
int count = 0;
for (int i = 0; stringLength[i] != '\0'; i++)
count++;
cout << "size of string is: " << count << endl;
return count;
}
void reverseString(int arraySize, char string2[])
{
int counter = 0;
char stringy[100];
for (int i = arraySize - 1; i >= 0; i--)
{
stringy[counter] = string2[i];
counter++;
}
stringy[counter] = '\0';
cout << stringy << endl;
}
int main()
{
char str[] = "string";
reverseString(stringCounter(str),str);
return 0;
}
I am trying to learn some of this beautiful language but I've got stuck on this. Problem is: Why does the last count shows only Witaj PJC not Witaj Cpp PJC? As you see function app has to append transformed 2nd word to 1st one.
Thanks for any help.
If you could give me any good tutorial about pointers I would appreciate that. Thanks!
#include <iostream>
#include <string.h>
using namespace std;
void app(char *str2, char *str1){
for(int i =0; i < strlen(str2); i++){
*(str2++);
}
for(int i =0; i < strlen(str1); i++){
*(str1++);
}
for(int i =0; i < strlen(str1); i++){
*(str2)=*(str1);
*(str2)++;
*(str1)--;
}
}
int main()
{
char *str1 = "ppC ";
char str2[20] = "Witaj";
cout << str2 << endl; // Witaj
app(str2, str1);
cout << str2 << endl; // Witaj Cpp shows WitCpp
app(str2, "CJP ");
cout << str2 << endl; // Witaj Cpp PJC shows WitPJ
return 0;
}
Your problem is this sort of loops:
for(int i =0; i < strlen(str2); i++){
*(str2++);
}
You can't move your pointer with str2++ and expect that strlen(str2) still returning the lenght of the original one.
For loop variables, in each iteration:
i str2 strlen(str2) condition
Iteration 1 0 Witaj 5 0 < 5 Ok
Iteration 2 1 itaj 4 1 < 4 Ok
Iteration 3 2 taj 3 2 < 3 Ok
Iteration 4 3 aj 2 3 < 2 Exit at 3rd character!!
Thus.. you only "move" your pointer 3 bytes.
Change your app function for that one:
void app(char *str2, char *str1){
int nstr2 = strlen(str2);
int nstr1 = strlen(str1);
for(int i =0; i < nstr2; i++){
*(str2++);
}
for(int i =0; i < nstr1; i++){
*(str1++);
}
for(int i =0; i < nstr1; i++){
*(str2++)=*(--str1);
}
}
Anyway... this program is only for academic porpouses or you are thinking use it professionally?
And for some functioning code for just string appending, i scribbled this...
Note that you should make a const call instead, and if you want to reverse one of the strings (a bit unclear from your question) it should be done prior to appending.
Example of string append (rather unsafely and rudimentary) using a new allocation:
char* app(char *str2, char *str1){
char* appendedstring = (char*)malloc(sizeof(char)*20);
char *temp = str1;
char *temp2 = str2;
int stringlen1 = strlen(str1);
int stringlen2 = strlen(str2);
//Copy string 1
for (int i = 0; i < stringlen2; i++){
appendedstring[i] = *temp2;
temp2++;
}
//Append string 2
for (int i = 0; i < stringlen1 + 1; i++){
appendedstring[stringlen2 + i] = *temp;
temp++;
}
return appendedstring;
}
int main()
{
int t;
char *str1 = "ppC ";
char str2[20] = "Witaj";
cout << str1 << endl;
cout << str2 << endl; // Witaj
char* newstr = app(str2, str1);
cout << newstr << endl; // Witaj Cpp shows WitCpp
char* newstr2 = app(str2, "CJP ");
cout << newstr2 << endl; // Witaj Cpp PJC shows WitPJ
return 0;
}