10 Replies - 469 Views - Last Post: 02 September 2013 - 02:31 AM Rate Topic: -----

#1 dexstar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-August 13

C - String manipulation

Posted 01 September 2013 - 02:14 AM

I have a function that gets input from stdin like so:

Student* readOneStudent()
{
    char buffer[MAX_LINE_LENGTH];  // Buffer into which we read a line from stdin
    Student* student = NULL;       // Pointer to a student record from the pool

    // Read a line, extract name and age

    char* cp = fgets(buffer, MAX_LINE_LENGTH, stdin); // reads whole line
    if (cp != NULL) {           // Proceed only if we read something
        char* commaPos = strchr(buffer, ','); // searches for ',' returns a pointer
        if (commaPos != NULL && commaPos > buffer) {
            int age = atoi(commaPos + 1); // converts string into integer type
            *commaPos = '\0';  // null-terminate the name
            student = newStudent(buffer, age);
        }
    }
    return student;
}


This function accepts input in the format:

Quote

Fred Nurk, 21


How can I modify it so that the input format is like:

Quote

21, Fred Nurk


?

So far I can get the age by changing

 int age = atoi(commaPos - 2); 


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: C - String manipulation

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: C - String manipulation

Posted 01 September 2013 - 02:54 AM

Think about what you're currently doing, you say *commaPos = '\0';. This effectively makes two strings, e.g.
Fred Nurk\0 21\0
or
21\0 Fred Nurk\0



So, buffer points to the first string and commaPos + 1 points to the second.
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: C - String manipulation

Posted 01 September 2013 - 03:02 AM

Replace s1[] with buffer[].

#include <stdio.h>

int main(void) {

   char s1[80]={"Fred Nurk, 21"};
   char fname[30],lname[30];
   int age;

   sscanf(s1, "%s %s %d",fname, lname, &age);
   printf("Age: %d,  First name: %s  Last name: %s \n",age,fname,lname);
}


Was This Post Helpful? 0
  • +
  • -

#4 dexstar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-August 13

Re: C - String manipulation

Posted 01 September 2013 - 03:08 AM

View Postbaavgai, on 01 September 2013 - 02:54 AM, said:

Think about what you're currently doing, you say *commaPos = '\0';. This effectively makes two strings, e.g.
Fred Nurk\0 21\0
or
21\0 Fred Nurk\0



So, buffer points to the first string and commaPos + 1 points to the second.


I thought buffer pointed to the whole string. I'm not sure when to null terminate
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: C - String manipulation

Posted 01 September 2013 - 04:39 AM

Correct, the buffer points to the whole thing. However, c-strings stop evaluation when they hit \0.

Try this:
char *p, s[] = "foo bar";

p = s + 3;
printf("s = '%s', p='%s'\n", s, p);
*p = '\0'; /* throw in that stop char */
printf("s = '%s', p='%s'\n", s, p);
p++; /* move p up one to get something more interesting */
printf("s = '%s', p='%s'\n", s, p);



Results:
s = 'foo bar', p=' bar'
s = 'foo', p=''
s = 'foo', p='bar'



Understand, the memory allocated to s is holding both strings. However, string processing stops when it hits stop.
Was This Post Helpful? 0
  • +
  • -

#6 dexstar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-August 13

Re: C - String manipulation

Posted 01 September 2013 - 06:57 AM

View Postbaavgai, on 01 September 2013 - 04:39 AM, said:

Correct, the buffer points to the whole thing. However, c-strings stop evaluation when they hit \0.

Try this:
char *p, s[] = "foo bar";

p = s + 3;
printf("s = '%s', p='%s'\n", s, p);
*p = '\0'; /* throw in that stop char */
printf("s = '%s', p='%s'\n", s, p);
p++; /* move p up one to get something more interesting */
printf("s = '%s', p='%s'\n", s, p);



Results:
s = 'foo bar', p=' bar'
s = 'foo', p=''
s = 'foo', p='bar'



Understand, the memory allocated to s is holding both strings. However, string processing stops when it hits stop.


When you call

*p = '\0'; 


where does it get thrown? at the end of the string array?

Also does
p++;
go to the next index?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: C - String manipulation

Posted 01 September 2013 - 07:30 AM

Ah, well, we're talking pointers, here.

Hmm... for reference, the same code without pointers.
char s[] = "foo bar";

/* here, we get the address of third position of s */
printf("s = '%s', s[3]='%s'\n", s, &s[3]); 
s[3] = '\0'; /* same as *(s + 3) = '\0'; */
/* here, we use pointer arithmatic to get the same address */
printf("s = '%s', s[3]='%s'\n", s, s + 3);
printf("s = '%s', s[4]='%s'\n", s, s + 4);



Results:
s = 'foo bar', s[3]=' bar'
s = 'foo', s[3]=''
s = 'foo', s[4]='bar'



Understand, when you pass an array, even to printf, you're passing a pointer. A c-string is an array of characters. Arrays and pointers are pretty close friends.
A quick cheat sheet.
char *p;
p is the address
*p is the character at that address
p + 1 is the address of the second character
*(p + 1) is the character at that address
p[0] is the first character, equiv to *p
p[1] is the second character, equiv to *(p + 1) 
&p[1] is the address of second character, equiv to p + 1
note, in that last one, we've kind of gone there and back again



If this is confusing, and it can be, read all you can about pointers in C. They ARE confusing, at first. But once you get them, you'll have the key to understand all of C. To effectively use C, you MUST understand pointers.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#8 dexstar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-August 13

Re: C - String manipulation

Posted 01 September 2013 - 06:11 PM

Why does

char s[] = "foo bar";
printf("s = '%s', s[3]='%s'\n", s, &s[3]);




Print

s = 'foo bar', s[3]=' bar'



I was under the impression that it would print a single char stored at s[3] (index 3 of the string array)

Chars: Foo bar
Index: 0123456

e.g ' ';
Was This Post Helpful? 0
  • +
  • -

#9 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: C - String manipulation

Posted 01 September 2013 - 06:22 PM

It prints the string, because you used the format specifier for strings: %s.
If you want a single char, please use the correct char format specifier: %c.

The %s used with &s[3] is an error. Why would you want the address of (&) a char, printed out as a string?

Read up in your help files, or book, on format specifiers for printf().
Was This Post Helpful? 1
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: C - String manipulation

Posted 01 September 2013 - 06:26 PM

baavgai told you: printf prints the entire string starting from the pointer to the first character and ending with then null terminator. It does not print just a character.
Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: C - String manipulation

Posted 02 September 2013 - 02:31 AM

Sorry, clearly I should have included & in the printf, which I didn't, e.g.
char s[] = "foo bar";
printf("s = '%s', &s[3]='%s'\n", s, &s[3]);


Print
s = 'foo bar', &s[3]=' bar'


You are correct, s[3] will return a single character. But &s[3] will return the address of that character. And, from C's perspective, an address to a character is a string.

An array is one or more like elements. This is implemented as a block of memory that is sizeof(element) * numberOfElements large. In code, char * could just be a single element, or it could be part of an array. Or, perhaps another way, every variable is an array of 1 unless stated otherwise. Ultimately, it's all blocks of memory. Unlike most programming languages, C shows this to you and expects you to like it. ;)

Written another way:
char s[] = "foo bar";
char *p = &s[3];
printf("s = '%s', p]='%s'\n", s, p);



Hope this helps.

View PostAdak, on 01 September 2013 - 09:22 PM, said:

The %s used with &s[3] is an error. Why would you want the address of (&) a char, printed out as a string?


Sorry, no, it is absolutely not an error is this context.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1