finding length of characters in an assigned char array ??

This is a discussion on finding length of characters in an assigned char array ?? within the C Programming forums, part of the General Programming Boards category; hello , this is my first post here and i hope i can be helpful around here, so here is ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    finding length of characters in an assigned char array ??

    hello , this is my first post here and i hope i can be helpful around here, so here is my question

    lets say that i have a multidimensional array defined as char names[1] [1] , the first column is to write the name of the person and the second column is to write the surname. After i write the name, this is where the question begins actually, for every consonant i must replace it by 0 and for every vowel i must put 1 , for example, if i have a name such as Jack, it must be 0100 ,

    i've tried to write something on my own , but it didn't work

    my guess is that if i can find the number of the characters in the name entered, i can replace them by writing a for loop and a switch

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    If you've got "char names[1] [1]" then you've already got a bug. That entire array holds as much as a single char, nothing more.
    You want two names so the first number has to be at least 2.
    You want names of more than zero characters long, so the second number has to be something sensible like 32.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    sorry for that mistake, i fixed that mistake , so
    what needs to be done to find the length of the name that user entered?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    strlen function should help here
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    4
    ok , i wrote a program but it doesn't work,


    i am sorry if there are terrible mistakes here, but i'm just a beginner

    what i want the program to do is to have names[0] as aaa and to print out 222

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    int main()
    {
    char names [1][20];
    names[0]="aaa";
    char amk=(char)names[0];
    for(int i=0;i<=strlen(names[0]);i++){
    switch(amk){
    case 'a' :
    printf("2");
    break;
    default:
    printf("1");
    break;
    }
    }
    getch();
    return 0;
    }
    it says Lvalue is required in function main

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    fixed version:

    Code:
    ...
      char names [1][20]={{"aaa"}};
      for(int i=0;i<strlen(names[0]);i++){
        char amk=names[0][i];
        switch(amk){
          case 'a' :
          printf("2");
          break;
          default:
          printf("1");
          break;
        }
      }
    ...
    you cannot initialize a char array with a string litteral after its allocation (names[0]="aaa")
    do it during its allocation or by using strcpy() (for example) after its allocation.
    Be careful with indexes [0..strlen()-1] instead of [0..strlen()].
    At last, you must update the 'amk' value for each loop(char amk=names[0][i]).

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    4
    thank you for your help !!
    i know it went a little out of topic, but i got my answer so thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21