Текстуални низи - задачи

Tekstualni nizi - zadaci 

http://www.webnstudy.com/tema.php?id=rad-sa-stringovima-zadaci

Прашања:

1. How many types of representation are in the string?
a) 1
b) 2
c) 3
d) 4

2. What is the header file for the string class?
a) #include<ios>
b) #include<str>
c) #include<string>
d) #include<stio>

3. Which is used to return the number of characters in the string?
a) length
b) size
c) both size & length
d) name

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <cstring>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         char str1[10] = "Hello";
  7.         char str2[10] = "World";
  8.         char str3[10];
  9.         int  len ;
  10.         strcpy( str3, str1);
  11.         strcat( str1, str2);
  12.         len = strlen(str1);
  13.         cout << len << endl;
  14.         return 0;
  15.     }

a) 5
b) 55
c) 11
d) 10

5. Which method do we use to append more than one character at a time?
a) append
b) operator+=
c) data
d) both append & operator+=

6.  What is the output of the following code snippet?

1.#include <iostream>
#include <string>
using namespace std;

int main() {
string s1 {“QuizOrbit”};
for (int i = 0; i < s1.length(); i++)
{
s1[i]=toupper(s1[i]);
}
cout<<s1;
return 0;
}

2. #include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[50]="Hello", s2[50]="World";
strcat(s1, s2);
cout << s1 << endl;
return 0;
}

3. #include <iostream>
#include <string>
using namespace std;
int main()
{
string str={“Hello World”};
cout<<str[6];
return 0;
}

Програма со која се определува колку знаци во дадена низа знаци се букви

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char a[100];
int d,i,b=0;
cout<<"Vnesi niza "<<endl;
cin.getline(a,100);
d=strlen(a);
for(i=0;i<=d-1;i++)
if (isalpha(a[i])) b++;
cout<<"ima "<<b;
return 0;
}

Програма со која сите букви во дадена низа знаци се претвораат во мали.

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
 char a[100];
 int d,i;
cout<<"Vnesi niza "<<endl;
cin.getline(a,100);
d=strlen(a);
for(i=0;i<=d-1;i++)
 a[i]=tolower(a[i]);
for(i=0;i<=d-1;i++)
 cout<<a[i];
return 0;
}

Програма со која даден внесен текст преку тастатура се печати во обратен редослед.

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char tekst[100];
int i,dolzina;
cout << "Vnesi tekst: ";
cin.getline(tekst,100); 
 dolzina=strlen(tekst); 
cout<<"dolinata na tekstot e"<<dolzina<<endl; 
cout<<"Vneseniot tekst vo obraten redosled e :"<<endl;
for(i=dolzina-1;i>=0;i--) cout << tekst[i]; 
return 0;
}

Програма со која се проверува дали внесениот текст е палиндром или не. Палиндром е текст кој се чита исто во двете насоки – од лево кон десно и од десно кон лево

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char tekst[100],tekst_obratno[100];
bool palindrom;
int i,dolzina;
cout << "Vnesi tekst: ";
cin.getline(tekst,100);
dolzina=strlen(tekst);
for(int i=dolzina-1;i>=0;i--)
 tekst_obratno[i]=tekst[dolzina-i-1];
for(int i=0;i<=dolzina-1;i++)
 {
 if (tekst[i]==tekst_obratno[i])
 palindrom=true;
 else
 {
 palindrom=false;
 cout<<"Vneseniot tekst ne e palindrom"<<endl;
 break;
 }
 }
 if (palindrom==true)
 cout<<"Vneseniot tekst e palindrom"<<endl;
return 0;
}

Програма со која се проверува дали два внесени текста се еднакви или не.

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char tekst1[100],tekst[100];
int i,dolzina;
bool t;
cout << "Vnesi tekst 1: ";
cin.getline(tekst,100);
cout << "Vnesi tekst 2: ";
cin.getline(tekst1,100);
 t=strcmp(tekst, tekst1);
 if(t==0) cout<<"Tekstovite se ednakvi";
 else cout<<"tekstovite ne se ednakvi";
return 0;
}

Што ќе се прикаже со следните кодови 
1.
#include <iostream>
//#include <string.h>
using namespace std;
 
int main( ) {
      cout<<"Vnesete tekst"<<endl;
    string t;
    getline(cin,t);
    cout<<"_______"<<endl;
    for (int i=0; i < t.length();i++){
        cout<<t<<" "<<endl;
    }
   return 0;
}
2.
#include <iostream>
//#include <string>
using namespace std;
 
int main() 
{
    string  t;
       for(int i=1;i<=3;i++) {
               for(int j=1;j<=3;j++) {
                    if(i+j>3+1)
                    cout<<"IME "; 
                    else
                    cout<<"--- ";
                  }
          cout<<endl;
     }        
    return 0;
}
3.
#include <iostream>
#include <string>
 
using namespace std;
int main( )
{
    string vnes;
    cout<<"Vnesi tekst: ";
    getline(cin,vnesi);
     
    for(int i=vnesi.length();i>=0;i--)
    {
        cout<<vnesi[i]<<" ";
    }
      return 0;
}

4. a
#include <iostream>
using namespace std;
int main()
{
string word1 = "predmet";
string word2 = "redar";
cout << word1.substr(word1.find("re"),3) << " " << word2.substr(word2.find("re"),3) << endl;
return 0;
}

4.b
#include <iostream>
using namespace std;
int main()
{
string word;
word = "omilen predmet";
int num = 11;
cout << num/3 << "+" << num % 7 << endl;
cout << 11 << "111" << '1' << '1' << endl;
cout << word.substr(word.find("pr"),3) << "krasno" << endl;
return 0;
}

Внеси текст и знак . Преброј колку пати се јавува внесениот знак во текстот.

#include <iostream>
#include <string>
using namespace std;

int main(){
int brojac=0;
string s;
char z;
cout<<"Vnesi string/tekst ";
getline(cin, s);
cout<<"Vnesi znak ";
cin>>z;
for(int i=0; i < s.length(); i++) {
if(s[i] == z){
brojac++;
}
}
cout<<"Brojot na znaci "<<z<< " e "<<brojac<<endl;
return 0;
}

Внеси текст и преброј колку зборови има

#include <iostream>
#include <string>
using namespace std;

int main(){
string a;
cout<<"Vnesi tekst ";
getline(cin,a);
//int brojac = (a.length() > 0)? 1 : 0;
int brojac = a.length() ;
if (brojac>0) brojac =1;
else brojac=0;
for(int i = 0; i < a.length();i++){
if(a[i]==' '){
brojac++;  }
}
cout<<"Brojot na zborovi vo tekstot e: "<<brojac<<endl;
return 0;
}

No comments: