Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,176 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,373 people online right now. Registration is fast and FREE... Join Now!




string

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

string

kckc314
26 May, 2008 - 09:55 PM
Post #1

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

if my program reads the following strings(few sentences) as command line arguments into the program:
"I have just seen Sirs John and Nick!"
"I have just met Sirs Mike, Paul and chris."

from the program i want to only take and output the names come after Sirs or Sir. in this case it would be John, Nick, Mike, Paul and chris. below are some codes that i am trying to code but still stuck. and also the sentences are stored using array of pointers, some word like Mike and Nick contains a full stop and exclaimation character. how do i get rid of those characters when i store the name? is it a good idea to store those name into a char buffer[80] as i want to do sorting later? if Mike has been store from the first sentence, the rest of the sentence comes after also has a Sir Mike, how do i counter check with the existing array, if Mike already existed then don't assign into it?
CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
    
    int i;
    for (i = 1; i < argc; i++)
        if (argv[i] == 'Sirs')
      
    system("PAUSE");
    return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

kckc314
RE: String
27 May, 2008 - 12:37 AM
Post #2

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

regarding to the first post, i have add some codes as following:
CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
    
    char sirs[80];
    int i, j, k = 0;
    for (i = 1; i < argc; i++) {
        for (j = 1; j < argc; j++) {
            if (argv[i] == 'Sir')
               sirs[k++] = argv[i + 1];
            else
            if (argv[i] == 'Sirs' && j > i && argv[j] != 'and')
               sirs[k++] = argv[j];
            else
            if (argv[i] == 'Sirs' && j > i && argv[j] == 'and')
               sirs[k++] = argv[j + 1];
               break;
        }
    }
    
    for (int x = 0; x < k; x++)
        printf("%s  ", sirs[x]);
      
    system("PAUSE");
    return EXIT_SUCCESS;
}

but i get the below error msg and warnings upon compiling. pls advise.
\solve.c:26:28: warning: multi-character character constant
In function `main':
warning: comparison between pointer and integer
warning: assignment makes integer from pointer without a cast
warning: multi-character character constant
warning: comparison between pointer and integer
warning: multi-character character constant
warning: comparison between pointer and integer

warning: assignment makes integer from pointer without a cast
warning: multi-character character constant
warning: comparison between pointer and integer
warning: multi-character character constant
warning: comparison between pointer and integer
warning: assignment makes integer from pointer without a cast

Execution terminated
User is offlineProfile CardPM
+Quote Post

kckc314
RE: String
27 May, 2008 - 05:06 AM
Post #3

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

pls find the following code as the output is "The Sirs are: (null)" which is incorrect. pls advise thanks in advance.
CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIRS 25

int get_sirs(char *sirs[]);
void sort(char *p[], int n);
void print_sirs(char *p[], int n);

char all_sirs[25];
char *sirs[MAXSIRS];

int main(int argc, char *argv[]) {
    
    int number_of_sirs;
    int i, j, k = 0, m;
    
    /* To find out the number of Sirs involved in the puzzle */
    for (i = 1; i < argc; i++) {
        for (j = 1; j < argc; j++) {
            if (argv[i] == "Sirs" && j > i && argv[j] != "and")
               all_sirs[k++] = *argv[j];
            else
            if (argv[i] == "Sirs" && j > i && argv[j] == "and")
               all_sirs[k++] = *argv[j + 1];
               break;
        }
    }
    
    /* To find out the number of Sir involved in the puzzle */
    for (m = 1; m < argc; m++)
        if (argv[m] == "Sir")
           all_sirs[k++] = *argv[m + 1];
    
    number_of_sirs = get_sirs(sirs);
    
    if (number_of_sirs < 0) {
       printf("Memory allocation error.\n");
       exit(-1);
    }
    
    sort(sirs, number_of_sirs);
    print_sirs(sirs, number_of_sirs);
      
    system("PAUSE");
    return EXIT_SUCCESS;
}

/* To compute how many line of names of each Sirs for sorting purposes */
int get_sirs(char *sirs[]) {
    int n = 0;
    
    if ((sirs[n] = (char *)malloc(strlen(all_sirs)+1)) == NULL)
       return -1;
    strcpy(sirs[n++], all_sirs);
    
    return n;
}

/* To sort the name of each Sirs in ascending order */
void sort(char *p[], int n) {
    
     int a, b;
     char *tmp;
    
     for (a = 1; a < n; a++) {
         for (b = 0; b < n - 1; b++) {
             if (strcmp(p[b], p[b + 1]) > 0) {
                tmp = p[b];
                p[b] = p[b + 1];
                p[b + 1] = tmp;
             }
         }
     }
}

/* Output the name of each Sirs */
void print_sirs(char *p[], int n) {
    
     int ctr;
    
     for (ctr = 0; ctr < n; ctr++)
         printf("The Sirs are: ");
         printf("%s ", p[ctr]);
         printf("\n");
}

User is offlineProfile CardPM
+Quote Post

joske
RE: String
27 May, 2008 - 08:52 AM
Post #4

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
A char can only store one character. You use a pointer to a list of chars. You should first allocate memory for it (using the function new) before copying the data into it, terefore you get (null) as output now in your code.

I think it is a much easier solution to use a vector of strings:
cpp
#include <vector>
#include <cstring>

std::vector<std::string>sirs;

sirs.push_back(std::string("John"));
sirs.push_back(std::string("Mike"));
sirs.push_back(std::string("Chris"));

int number_of_sirs = sirs.size();

for (int n = 0; n < number_of_sirs; n++)
{
printf("sirs[%i] = %s \n", n, sirs[n].c_str());
}



one other bug in your code: the part
cpp
     for (ctr = 0; ctr < n; ctr++)
printf("The Sirs are: ");
printf("%s ", p[ctr]);
printf("\n");


should be
cpp
     for (ctr = 0; ctr < n; ctr++)
{
printf("The Sirs are: ");
printf("%s ", p[ctr]);
printf("\n");
}


This post has been edited by joske: 27 May, 2008 - 08:53 AM
User is offlineProfile CardPM
+Quote Post

polymath
RE: String
27 May, 2008 - 02:09 PM
Post #5

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 415



Thanked: 4 times
Dream Kudos: 500
My Contributions
If your in c++, use the c++ string library. Simpler
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: String
27 May, 2008 - 02:15 PM
Post #6

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
QUOTE(polymath @ 27 May, 2008 - 10:09 PM) *

If your in c++, use the c++ string library. Simpler

I'm pretty sure that's C. Take a look at the libraries being used tongue.gif
User is online!Profile CardPM
+Quote Post

polymath
RE: String
27 May, 2008 - 02:17 PM
Post #7

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 415



Thanked: 4 times
Dream Kudos: 500
My Contributions
Oh, Right. Sorry for the extraneous post.
User is offlineProfile CardPM
+Quote Post

kckc314
RE: String
27 May, 2008 - 06:44 PM
Post #8

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

as i have changed this part of code to allocate memory storage for the input sentences. but the output is "The Sirs are: " as no name appear as output.

FYI, the input sentences that i use for command line argument is "I have just seen Sirs Mike and Chris! \"I am a Knave,\" whispered Sir Eleonore. Who is a Knight and who is a Knave?"
CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIRS 25

int get_sirs(char *sirs[]);
void sort(char *p[], int n);
void print_sirs(char *p[], int n);

char puzzle[200];
char *puzz[200];
char all_sirs[25];
char *sirs[MAXSIRS];

int main(int argc, char *argv[]) {
    
    int number_of_sirs;
    int i, j, k = 0, m, n = 0, p = 0, s;
    
    for (s = 1; s < argc; s++)
        puzzle[p++] = *argv[s];
        
    if ((puzz[n] = (char *)malloc(strlen(puzzle)+1)) == NULL) {
       printf("Memory allocation error.\n");
       exit(1);
    }
    strcpy(puzz[n++], puzzle);
    
    /* To find out the number of Sirs involved in the puzzle */
    for (i = 1; i < argc; i++) {
        for (j = 1; j < argc; j++) {
            if (puzz[i] == "Sirs" && j > i && puzz[j] != "and")
               all_sirs[k++] = *puzz[j];
            else
            if (puzz[i] == "Sirs" && j > i && puzz[j] == "and")
               all_sirs[k++] = *puzz[j + 1];
               break;
        }
    }
    
    /* To find out the number of Sir involved in the puzzle */
    for (m = 1; m < argc; m++)
        if (puzz[m] == "Sir")
           all_sirs[k++] = *puzz[m + 1];

User is offlineProfile CardPM
+Quote Post

joske
RE: String
27 May, 2008 - 11:56 PM
Post #9

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
Can you also post the second half of your code?
User is offlineProfile CardPM
+Quote Post

kckc314
RE: String
28 May, 2008 - 12:15 AM
Post #10

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

pls find the previous post as i have posted a complete code. the post #8 is the top part of post #3.

This post has been edited by kckc314: 28 May, 2008 - 12:17 AM
User is offlineProfile CardPM
+Quote Post

joske
RE: String
28 May, 2008 - 03:41 AM
Post #11

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
huh.gif
so, did you adjsut the following?

QUOTE(joske @ 27 May, 2008 - 06:52 PM) *

one other bug in your code: the part
cpp
     for (ctr = 0; ctr < n; ctr++)
printf("The Sirs are: ");
printf("%s ", p[ctr]);
printf("\n");


should be
cpp
     for (ctr = 0; ctr < n; ctr++)
{
printf("The Sirs are: ");
printf("%s ", p[ctr]);
printf("\n");
}



User is offlineProfile CardPM
+Quote Post

kckc314
RE: String
28 May, 2008 - 05:18 AM
Post #12

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

yes i have adjusted that by including the opening and closing curly bracket in the for loop. before that the output was "The Sirs are: (null)". now the output is "The Sirs are: " as still no name appear.

i think the problem is here but i don't know how to fix it. here is the explanation.

the problem is in array of pointers, in a for loop when i reach a particular word let say "Sirs", then i want to store the word next to it into an array. so i have to use for loop to read the whole sentence word by word. the sentence is the command line argument, when the program runs it will all be stored into char *argv[]. so i need to look for the "Sirs" or "Sir" word along the sentences using for loop to loop through char *argv[]. but if i write like the code as mentioned above, it compares with the pointer address, not with the string pointed by the pointer address. is there anything wrong in the code? pls advise. thanks in advance.

FYI, if i write the below code, it can print out all the strings word by word in each element of the array pointed to by the pointer address, but how come when i do like above code, it will only compare with the pointer address?
CODE

for (int x = 1; x < argc; x++)
     printf("%s", argv[x]);


to provide the current code what i have now.
CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIRS 25

int get_sirs(char *sirs[]);
void sort(char *p[], int n);
void print_sirs(char *p[], int n);

char all_sirs[25];
char *sirs[MAXSIRS];

int main(int argc, char *argv[]) {
    
    int number_of_sirs;
    int i, j, k = 0, n;
    
    /* To find out the number of Sirs involved in the puzzle */
    for (i = 1; i < argc; i++) {
        for (j = 1; j < argc; j++) {
            if (argv[i] == "Sirs" && j > i && argv[j] != "and")
               for (n = 0; n <= k; n++) {
                   if (all_sirs[n] == *argv[j])
                      break;
                      if (all_sirs[n] != *argv[j])
                         continue;
                      all_sirs[k++] = *argv[j];
               }
            else
            if (argv[i] == "Sirs" && j > i && argv[j] == "and")
               for (n = 0; n <= k; n++) {
                   if (all_sirs[n] == *argv[j + 1])
                      break;
                      if (all_sirs[n] != *argv[j + 1])
                         continue;
                      all_sirs[k++] = *argv[j + 1];
               }
            else
            if (argv[i] == "Sir")
               for (n = 0; n <= k; n++) {
                   if (all_sirs[n] == *argv[i + 1])
                      break;
                      if (all_sirs[n] != *argv[i + 1])
                         continue;
                      all_sirs[k++] = *argv[i + 1];
               }
        }
    }
    
    number_of_sirs = get_sirs(sirs);
    
    if (number_of_sirs < 0) {
       printf("Memory allocation error.\n");
       exit(-1);
    }
    
    sort(sirs, number_of_sirs);
    print_sirs(sirs, number_of_sirs);
      
    system("PAUSE");
    return EXIT_SUCCESS;
}

/* To compute how many line of names of each Sirs for sorting purposes */
int get_sirs(char *sirs[]) {
    int n = 0;
    
    if ((sirs[n] = (char *)malloc(strlen(all_sirs)+1)) == NULL)
       return -1;
    strcpy(sirs[n++], all_sirs);
    
    return n;
}

/* To sort the name of each Sirs in ascending order */
void sort(char *p[], int n) {
    
     int a, b;
     char *tmp;
    
     for (a = 1; a < n; a++) {
         for (b = 0; b < n - 1; b++) {
             if (strcmp(p[b], p[b + 1]) > 0) {
                tmp = p[b];
                p[b] = p[b + 1];
                p[b + 1] = tmp;
             }
         }
     }
}

/* Output the name of each Sirs */
void print_sirs(char *p[], int n) {
    
     int ctr;
    
     for (ctr = 0; ctr < n; ctr++) {
         printf("The Sirs are: ");
         printf("%s ", p[ctr]);
         printf("\n");
     }
}


This post has been edited by kckc314: 28 May, 2008 - 05:22 AM
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 12/4/08 10:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month