College problem

Is anyone willing to help me solve (and explain) a few problems that I have for homework in C++?

Thanks.
You know what? Someone should create a web forum where people can post their problems, and then other people can help them solve them.

That would be a great idea!
I have a great name for you MikeyBoy: "cplusplus.com"! Has a nice ring to it.

@ A991ES: Sorry for the sarcasm but that's what these kind of sites are here for. Ask your questions (with a proper attempt made to solve it yourself showing your code and error tracebacks of course... ;-)
Well, if I knew how to solve it, maybe I wouldn't ask for help. English is not my first language and I just started studying C++ so I'm a bit stuck.
Hopefully he's not stuck on the opposite ledge of the sarchasm!
Not sure about who you're talking (@Texan40), but I'm a girl.
Are you a 'gig-em' sort of A991ES? o_O

Post what you have so far on your homework and we will try to help.
Not sure what you mean by 'gig-em', but my username is actually a kind of calculator and the 'A' is random.


#include <stdio.h>
#include "mystr.h"

int main ()
{
char a [100], B [100], C [100], C [100];

// Function should load line of text from stdin in committed buffer
// Puts ("Enter three strings separated by ENTER");
printf ("\ n");
myreadline (a);
printf ("b: \ n");
myreadline (b);
printf ("c: \ n");
myreadline (c);
printf ("\ n");

// Function should return the length of the string
printf ("The length of the string a: %d \n", mystrlen (a));
printf ("The length of the string b: %d \n", mystrlen (b));
printf ("The length of the string c: %d \n", mystrlen (c));

// Function should copy the contents of the first entered to the second string, returns
// How many characters were copied
int n = mystrcpy (a, d);
printf ("\nSTRING is copied in d and is: %s \n", d);
printf ("copied %d characters \n", n);

// Function should be checked whether the strings are equal, returns an int
if (mystrcmp (a, b))
printf ("The strings a and b are the same! \n");
else
printf ("The strings a and b are different! \n");

// Function should check whether the first string is contained within another, returns
// Index on which the first sign of found string, or a negative number
// If not found
puts ("\n Search if any string is part of another string");
puts ("Enter the first string in which looking, and then the string you're looking for");
// Char big [] = "This is a big string in which the asking!";
// Sub char [] = "large"
char big [100], sub [100];
printf ("'main' string: \n"); myreadline (big);
printf ("'substring': \ n"); myreadline (sat);
int loc = mysubstring (big, Sat);
if (loc> = 0)
printf ("The string '%s' in the string '% s' is found on %d. place. \ n", sub, big, loc);
else
printf ("The string '% s' is not part of the string'% s' \ n ', sub, big);

char e [100], f [100], g [100];
// Load text in e and copy it to the f and g string
printf ("\ nPlease enter a text which you'll change the character: \ n");
myreadline (e);
mystrcpy (e, f);
mystrcpy (e, g);

// Function should all lower case letters in the string set to uppercase
mytoupper (e);
printf ("CAPS:%s \n", s);

// Function should be all upper-case letters set in lower case
mytolower (f);
printf ("case sensitive:% s \ n", f);

// Function should be all upper case transferred to lower case, but all in small-sized
myinvertcase (g);
printf ("inverted letter:% s \ n", g);


return 0;
}
Ah, ok. "Aggies" (how I first read your name) are from a college here in Texas. Moving along...

1) If you use the code tags (<> icon to right of composition box) it will format it much like an IDE and make it much easier for us to read.

2) You have lots of functions you are getting from mystr.h but you didn't post it.

3) When you post code with no questions it makes it hard to know what you need help with.
Oh, thanks!
Well, the functions are the ones that I don't know how to write.
I can show you some (possible) implementations of a few
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void mytolower(char &c) // function takes a character and converts to lower-case version if possible
{
  if(c >= 'A' && c <= 'Z') // char is between 'A' and 'Z'
   c += 32; // adding 32 to upper-case ASCII character gives you lower-case
  // if above statement was false, no conversion necessary
}

void myreadline(char *cs) // reads user input till the enter key is pressed, stores in cs
{
  unsigned int idx = 0; // to keep track of cs index
  char c = 0; // to get from input buffer
  while(c != '\n') // keep grabbing characters until user hits return key
  {
    std::cin.get(c);
    cs[idx] = c; // put character in cs
    idx++; // increment index counter
  }
  cs[idx] = 0; // convert endline to null
}
Last edited on
closed account (yR9wb7Xj)
Hey A99IES, you should sign up for Udemy, and search up for c++ beginners and click on the free section. And take the course with 5 stars on it. The instructor has video lectures from the beginning of the course all the way through the end of you're college career. He explains functions very well, I'm new to c++ and I know people on here are not very helpful and some just think we are just trying cheat our way through college but if you really want to learn go to that site Udemy.com. I'm learning alot from there and also use tutorials on this forum it really helps alot. Good luck on becoming a programmer! I would help, but I haven't learn functions yet.
Thanks for the examples. I have another question about the function that is searching for a string in another string. I wrote this code, but I know that's now going to give me the right result. I'm searching the location of the second string in the first one. So, my question is: how do I find the same string not the same letters?

1
2
3
4
5
6
7
8
9
int mysubstring (char a[100], char b[100]){
    int i;
    int loc;
    for (i=0;i<100;i++){
        if(a[i]==b[i])
        loc=i;
    }
    return loc;
}
std::string::find should help you here. This is an example:
1
2
3
4
5
6
7
string str ("There are two needles in this haystack.");
string str2 ("needle");

if (str.find(str2) != string::npos)
{
//.. found.
}
Think about the procedure you'd use for determining if a string was contained in a bigger string just by visual comparison. First you'd look for the first letter of the substring in the string, then you'd go one-by-one through the string to see if the whole substring was contained. A possible solution could be:

(I'm assuming that the character arrays passed in are null-terminated, you could also assume length if you have that information)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int mysubstring(char *str, char *sub) // find sub-string within string
{
   int i = 0;
   while(str[i] != 0) // process till end of str (null)
  {
      if(str[i] == sub[0]) // character match with first of sub
      {
         int m = i, s = 0; // keep i as placeholder for initial character match
         while(sub[s] != 0) // process till end of sub (null)
         {
            if(str[m] != sub[s]) break; // not substring, break back into main loop
            m++; s++; // check letter by letter str[m] => sub[s]
         }
         if(sub[s] == 0) return i; // whole substring found in string, return initial position
      }
      i++; // no match, increment str index and try again
  }
  return -1; // str end reached and no match found
}
string::find does that for you. No need to search character by character, read the above code again, it searches for the whole term "needles" inside of "There are two needles in this haystack."
@R23MJ: I'm assuming the prof wants them to "roll their own" as so many profs do. I probably should have asked tho ;)
soooooo, I'm kind of finished, but there's no output. Not sure where I messed up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include "mystr.h"

int main ()
{
char a [100], B [100], C [100], C [100];

// Function should load line of text from stdin in committed buffer
// Puts ("Enter three strings separated by ENTER");
printf ("\ n");
myreadline (a);
printf ("b: \ n");
myreadline (b);
printf ("c: \ n");
myreadline (c);
printf ("\ n");

// Function should return the length of the string
printf ("The length of the string a: %d \n", mystrlen (a));
printf ("The length of the string b: %d \n", mystrlen (b));
printf ("The length of the string c: %d \n", mystrlen (c));

// Function should copy the contents of the first entered to the second string, returns
// How many characters were copied
int n = mystrcpy (a, d);
printf ("\nSTRING is copied in d and is: %s \n", d);
printf ("copied %d characters \n", n);

// Function should be checked whether the strings are equal, returns an int
if (mystrcmp (a, b))
printf ("The strings a and b are the same! \n");
else
printf ("The strings a and b are different! \n");

// Function should check whether the first string is contained within another, returns
// Index on which the first sign of found string, or a negative number
// If not found
puts ("\n Search if any string is part of another string");
puts ("Enter the first string in which looking, and then the string you're looking for");
// Char big [] = "This is a big string in which the asking!";
// Sub char [] = "large"
char big [100], sub [100];
printf ("'main' string: \n"); myreadline (big);
printf ("'substring': \ n"); myreadline (sat);
int loc = mysubstring (big, Sat);
if (loc> = 0)
printf ("The string '%s' in the string '% s' is found on %d. place. \ n", sub, big, loc);
else
printf ("The string '% s' is not part of the string'% s' \ n ', sub, big);

char e [100], f [100], g [100];
// Load text in e and copy it to the f and g string
printf ("\ nPlease enter a text which you'll change the character: \ n");
myreadline (e);
mystrcpy (e, f);
mystrcpy (e, g);

// Function should all lower case letters in the string set to uppercase
mytoupper (e);
printf ("CAPS:%s \n", s);

// Function should be all upper-case letters set in lower case
mytolower (f);
printf ("case sensitive:% s \ n", f);

// Function should be all upper case transferred to lower case, but all in small-sized
myinvertcase (g);
printf ("inverted letter:% s \ n", g);


return 0;
} 


and this is mystr.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
void mytolower(char a[100]) 
{
    int i;
    for(i=0;i<100;i++){
  if((int)a >= 'A' && (int)a <= 'Z')
   a += 32; 
    }
}

void myreadline(char *a) 
{
  unsigned int i = 0; 
  char c = 0; 

  while(c != '\n') 
  {
    c=getchar();
    a[i] = c; 
    i++; 
  }
  a[i] = 0;
  printf("%s", a);
}

void mytoupper(char a[100]) 
{
    int i;
    for (i=0;i<100;i++){
  if((int)a>= 'a' && (int)a <= 'z')
   a -= 32; 
}
}

int mystrlen(char a[100]){
int i; 
int br=0;
for (i=0;i<100;i++){
    if(a[i]=='\0')
    break;
    if(a[i] != '\0')
    br++;
    
}
    return br-1;
}

int mystrcpy(char a[100], char b[100]){
    int i;
     while((a[i]!= '\0')&&(b[i]!='\0'))
     b[i]==a[i];
     b++
        ;
    return b[i];
}

int mystrcmp(char a[100], char b[100]){
    int i;
    for (i=0;i<100;i++)
    if (a[i]==b[i])
    return 0;
}

void myinvertcase(char a[100]){
    int i;
    for(i=0;i<100;i++){
        if((int)a>= 'a' && (int)a <= 'z')
        a -= 32;
        if((int)a>= 'A' && (int)a <= 'A') 
        a += 32;
    }
}

int mysubstring (char a[100], char b[100]){
   int i = 0;
   while(a[i] != 0)
  {
      if(a[i] == b[0]) 
      {
         int m = i, s = 0; 
         while(b[s] != 0) 
         {
            if(a[m] != b[s]) break; 
            m++; s++; 
         }
         if(b[s] == 0) return i;
      }
      i++;
  }
  return -1;
}
char a [100], B [100], C [100], C [100];


This line may be the culprit, should it be char a [100], b [100], c [100], d [100]; ?

Also, your code formatting is not the best, it's hard to read.

Are you getting any warnings? Try using break points to examine variables as the program runs. This may help.
Topic archived. No new replies allowed.